Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将可观察集合中的项目显示为循环链表_C#_Wpf_Collections_Observablecollection - Fatal编程技术网

C# 将可观察集合中的项目显示为循环链表

C# 将可观察集合中的项目显示为循环链表,c#,wpf,collections,observablecollection,C#,Wpf,Collections,Observablecollection,我有一个可观察的集合,由项目和按钮填充,这些项目按ID降序排列 ocItems = new ObservableCollection<Item>(); IQueryable<Item> Query = _context.Item.OrderByDescending(s=>s.ItemID); ocItems = new ObservableCollection<Item>(Query ); ocItems=新的ObservableCollection(

我有一个可观察的集合,由项目和按钮填充,这些项目按ID降序排列

ocItems = new ObservableCollection<Item>();
IQueryable<Item> Query = _context.Item.OrderByDescending(s=>s.ItemID);
ocItems = new ObservableCollection<Item>(Query );
ocItems=新的ObservableCollection();
IQueryable查询=_context.Item.OrderByDescending(s=>s.ItemID);
ocItems=新的ObservableCollection(查询);
我希望以以下方式显示每次单击中每个项目的信息:

第一次单击显示项1信息,第二次单击显示 项目2信息。。。。第五次单击显示项目5 信息,第六次单击显示项目1信息。。和 等等

如何将可观察集合中的项目显示为循环链表? 当我显示第二项时,如何在列表末尾显示第一项


希望这个清晰的

只需重置索引运算符值:

使用系统;
使用System.Collections.ObjectModel;
公共课程
{
公共静态void Main()
{
var项目=新[]
{
新项{Id=1,Value=“Hello”},
新项{Id=2,Value=“World!”},
};
var集合=新的可观察集合(项目);
对于(int i=0;i<10;i++)
{
var item=集合[i%collection.Count];
var message=String.Format(“{0}:{1}”,item.Id,item.Value);
控制台写入线(消息);
}
}
}
公共类项目
{
公共int Id{get;set;}
公共字符串值{get;set;}
}

您可以将每个项目的
标记设置为属性的索引;比如:

//on click event:
(sender as TextBlock).Tag = ((int)((sender as TextBlock).Tag) + 1 ) % MAX;
或:


然后在
类中使用反射或方法(
GetPropertyValueAt
)检索目标值:

public string GetPropertyValueAt(int index)
{
     switch(index % MAX)
     {
          //default:
          //return prop1;
     }
}
或:

如果您的
项目
是一个链接列表,则:

public string GetPropertyValueAt(int index)
{
    return this.ElementAt(index % MAX);
}

然后将
标签添加到每个项目的绑定中:

<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>
不要忘记将转换器添加到窗口的资源中


请注意,
%MAX
的用法是重复的,以确保在任何情况下循环行为。

如何使用
列表[clickCount%list.length]
?如何填充可观察对象collection@TheGeneralocItmes=新的ObservableCollection(查询);您描述的数据结构不是队列,它更像是循环链表。要循环普通集合中的项目,请保留当前显示索引的记录,每次单击时递增该索引,当到达末尾时,将索引重置为0。不需要排序necessary@ASh谢谢,我更新了我的问题标题。谢谢你的回答,但我想在每次单击中显示一个项目,在下一次单击中显示第二个。。。所以on@AbdulsalamElsharif:然后将
i
重命名为
clickCount
并增加
onButtonClicked()
中的值。不要期望得到你的问题的完整解决方案。此答案显示了当索引值高于列表计数时,如何循环浏览列表。如何以及何时使用这些信息是你的工作。谢谢你的回答,我感谢你的帮助。@B谢谢你的回答,我认为奥利弗的回答更简单。
public string GetPropertyValueAt(int index)
{
    this.GetType().GetProperties()[index % MAX];
}
public string GetPropertyValueAt(int index)
{
    return this.ElementAt(index % MAX);
}
<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;  
    }  
}