Browse Source

Added IEnumerable extension methods.

As we're working on a non-generic IEnumerable in ItemsControl related
controls.
pull/58/head
Steven Kirk 11 years ago
parent
commit
18a976dc55
  1. 1
      Perspex.Controls/Perspex.Controls.csproj
  2. 65
      Perspex.Controls/Primitives/SelectingItemsControl.cs
  3. 76
      Perspex.Controls/Utils/IEnumerableUtils.cs

1
Perspex.Controls/Perspex.Controls.csproj

@ -92,6 +92,7 @@
<Compile Include="UserControl.cs" /> <Compile Include="UserControl.cs" />
<Compile Include="Templates\TemplateExtensions.cs" /> <Compile Include="Templates\TemplateExtensions.cs" />
<Compile Include="TextWrapping.cs" /> <Compile Include="TextWrapping.cs" />
<Compile Include="Utils\IEnumerableUtils.cs" />
<Compile Include="Utils\StringUtils.cs" /> <Compile Include="Utils\StringUtils.cs" />
<Compile Include="TopLevel.cs" /> <Compile Include="TopLevel.cs" />
<Compile Include="PopupRoot.cs" /> <Compile Include="PopupRoot.cs" />

65
Perspex.Controls/Primitives/SelectingItemsControl.cs

@ -6,12 +6,11 @@
namespace Perspex.Controls.Primitives namespace Perspex.Controls.Primitives
{ {
using System; using Perspex.Controls.Utils;
using System.Collections;
using System.Linq;
using Perspex.Input; using Perspex.Input;
using Perspex.VisualTree; using Perspex.VisualTree;
using System.Collections.Generic; using System;
using System.Linq;
public abstract class SelectingItemsControl : ItemsControl public abstract class SelectingItemsControl : ItemsControl
{ {
@ -31,7 +30,7 @@ namespace Perspex.Controls.Primitives
if (control != null) if (control != null)
{ {
control.SelectedItem = control.GetItemAt((int)x.NewValue); control.SelectedItem = control.Items.ElementAt((int)x.NewValue);
} }
}); });
@ -58,58 +57,6 @@ namespace Perspex.Controls.Primitives
set { this.SetValue(SelectedItemProperty, value); } set { this.SetValue(SelectedItemProperty, value); }
} }
protected static int GetIndexOfItem(IEnumerable items, object item)
{
var list = items as IList;
if (list != null)
{
return list.IndexOf(item);
}
else if (items != null)
{
int index = 0;
foreach (var i in items)
{
if (object.ReferenceEquals(i, item))
{
return index;
}
++index;
}
}
return -1;
}
protected static object GetItemAt(IEnumerable items, int index)
{
var list = items as IList;
if (list != null)
{
return list[index];
}
else if (items != null)
{
return items.Cast<object>().ElementAt(index);
}
return -1;
}
protected int GetIndexOfItem(object item)
{
return GetIndexOfItem(this.Items, item);
}
protected object GetItemAt(int index)
{
return GetItemAt(this.Items, index);
}
protected virtual void MoveSelection(FocusNavigationDirection direction) protected virtual void MoveSelection(FocusNavigationDirection direction)
{ {
var panel = this.Presenter?.Panel as INavigablePanel; var panel = this.Presenter?.Panel as INavigablePanel;
@ -209,7 +156,7 @@ namespace Perspex.Controls.Primitives
} }
else if (value > -1) else if (value > -1)
{ {
var count = control.Items.Cast<object>().Count(); var count = control.Items.Count();
return Math.Min(value, count - 1); return Math.Min(value, count - 1);
} }
} }
@ -242,7 +189,7 @@ namespace Perspex.Controls.Primitives
} }
else else
{ {
this.SelectedIndex = GetIndexOfItem(selected); this.SelectedIndex = this.Items.IndexOf(selected);
} }
} }
} }

76
Perspex.Controls/Utils/IEnumerableUtils.cs

@ -0,0 +1,76 @@
// -----------------------------------------------------------------------
// <copyright file="IEnumerableUtils.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Controls.Utils
{
using System;
using System.Collections;
using System.Globalization;
using System.Linq;
internal static class IEnumerableUtils
{
public static int Count(this IEnumerable items)
{
Contract.Requires<ArgumentNullException>(items != null);
var collection = items as ICollection;
if (collection != null)
{
return collection.Count;
}
else
{
return items.Cast<object>().Count();
}
}
public static int IndexOf(this IEnumerable items, object item)
{
Contract.Requires<ArgumentNullException>(items != null);
var list = items as IList;
if (list != null)
{
return list.IndexOf(item);
}
else
{
int index = 0;
foreach (var i in items)
{
if (object.ReferenceEquals(i, item))
{
return index;
}
++index;
}
return -1;
}
}
public static object ElementAt(this IEnumerable items, int index)
{
Contract.Requires<ArgumentNullException>(items != null);
var list = items as IList;
if (list != null)
{
return list[index];
}
else
{
return items.Cast<object>().ElementAt(index);
}
}
}
}
Loading…
Cancel
Save