将可观察集合中的项目显示为循环链表
Display Items in observable collection as circular linked list
我有一个由项目和按钮填充的 Observable 集合,这些项目按其 ID 降序排列
ocItems = new ObservableCollection<Item>();
IQueryable<Item> Query = _context.Item.OrderByDescending(s=>s.ItemID);
ocItems = new ObservableCollection<Item>(Query );
我想通过以下方式在每次点击中显示每个项目的信息:
The first click display Item1 infomation, the secound click display
the Item2 information .... The fifth click display the Item5
information, The sixth click display the Item1 information .. and
so on.
如何将可观察集合中的项目显示为循环链表?
当我显示第二个项目时,如何在列表末尾显示第一个项目?
希望清楚
只需重置您的索引运算符值 back to zero:
using System;
using System.Collections.ObjectModel;
public class Program
{
public static void Main()
{
var items = new []
{
new Item{ Id = 1, Value = "Hello" },
new Item{ Id = 2, Value = "World!" },
};
var collection = new ObservableCollection<Item>(items);
for(int i = 0; i < 10; i++)
{
var item = collection[i % collection.Count];
var message = String.Format("{0}: {1}", item.Id, item.Value);
Console.WriteLine(message);
}
}
}
public class Item
{
public int Id { get; set; }
public string Value { get; set; }
}
您可以将每个项目的Tag
设置为属性的索引;喜欢:
//on click event:
(sender as TextBlock).Tag = ((int)((sender as TextBlock).Tag) + 1 ) % MAX;
或:
//on click command:
item.currentPropertyIndex = (item.currentPropertyIndex + 1 ) % MAX;
然后使用反射或Item
中的方法(GetPropertyValueAt
)检索目标值 class:
public string GetPropertyValueAt(int index)
{
switch(index % MAX)
{
//default:
//return prop1;
}
}
或:
public string GetPropertyValueAt(int index)
{
this.GetType().GetProperties()[index % MAX];
}
如果你的Item
是一个链表,那么:
public string GetPropertyValueAt(int index)
{
return this.ElementAt(index % MAX);
}
然后在每一项的绑定中添加Tag
:
<ItemsControl ItemsSource="{Binding ocItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource propSelector}">
<Binding Path=./>
<Binding Path="Tag" RelativeSource="{RelativeSource Self}/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
使用转换器,您可以为绑定创建自定义功能:
public class PropSelector : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (values != null)
{
return (values[0] as Item).GetPropertyValueAt(values[1]);
}
return "";
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
不要忘记将转换器添加到 window 的资源中。
请注意,重复使用 % MAX
以确保在任何情况下都有循环行为。
我有一个由项目和按钮填充的 Observable 集合,这些项目按其 ID 降序排列
ocItems = new ObservableCollection<Item>();
IQueryable<Item> Query = _context.Item.OrderByDescending(s=>s.ItemID);
ocItems = new ObservableCollection<Item>(Query );
我想通过以下方式在每次点击中显示每个项目的信息:
The first click display Item1 infomation, the secound click display the Item2 information .... The fifth click display the Item5 information, The sixth click display the Item1 information .. and so on.
如何将可观察集合中的项目显示为循环链表? 当我显示第二个项目时,如何在列表末尾显示第一个项目?
希望清楚
只需重置您的索引运算符值 back to zero:
using System;
using System.Collections.ObjectModel;
public class Program
{
public static void Main()
{
var items = new []
{
new Item{ Id = 1, Value = "Hello" },
new Item{ Id = 2, Value = "World!" },
};
var collection = new ObservableCollection<Item>(items);
for(int i = 0; i < 10; i++)
{
var item = collection[i % collection.Count];
var message = String.Format("{0}: {1}", item.Id, item.Value);
Console.WriteLine(message);
}
}
}
public class Item
{
public int Id { get; set; }
public string Value { get; set; }
}
您可以将每个项目的Tag
设置为属性的索引;喜欢:
//on click event:
(sender as TextBlock).Tag = ((int)((sender as TextBlock).Tag) + 1 ) % MAX;
或:
//on click command:
item.currentPropertyIndex = (item.currentPropertyIndex + 1 ) % MAX;
然后使用反射或Item
中的方法(GetPropertyValueAt
)检索目标值 class:
public string GetPropertyValueAt(int index)
{
switch(index % MAX)
{
//default:
//return prop1;
}
}
或:
public string GetPropertyValueAt(int index)
{
this.GetType().GetProperties()[index % MAX];
}
如果你的Item
是一个链表,那么:
public string GetPropertyValueAt(int index)
{
return this.ElementAt(index % MAX);
}
然后在每一项的绑定中添加Tag
:
<ItemsControl ItemsSource="{Binding ocItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource propSelector}">
<Binding Path=./>
<Binding Path="Tag" RelativeSource="{RelativeSource Self}/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
使用转换器,您可以为绑定创建自定义功能:
public class PropSelector : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (values != null)
{
return (values[0] as Item).GetPropertyValueAt(values[1]);
}
return "";
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
不要忘记将转换器添加到 window 的资源中。
请注意,重复使用 % MAX
以确保在任何情况下都有循环行为。