【问题标题】:Adding row numbers Windows phone 7 listbox添加行号 Windows phone 7 列表框
【发布时间】:2014-02-21 18:35:50
【问题描述】:

我有一个问题,我不知道该怎么办。我正在尝试制作一个简单的高分列表,我需要为每个分数获取数字 (1,2,3,4...)。

XAML:

<ListBox x:Name="ListBox" ItemsSource="{Binding Source.View}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                              <StackPanel Orientation="Horizontal">
                                   <TextBlock Text="{Binding ROW_NUMBER_HERE}"/>
                                   <TextBlock Text="{Binding Name}"/>
                                   <TextBlock Text="{Binding Score}"/>
                              </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
</ListBox>

C#

public ObservableCollection<Item> Items { get; set; }
public System.Windows.Data.CollectionViewSource Source { get; set; }

        public HighscorePage()
        {
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();

            if (store.FileExists("highscores"))
            {
                using (IsolatedStorageFileStream stream = store.OpenFile("highscores", FileMode.Open))
                {
                    var serializer = new DataContractSerializer(typeof(ObservableCollection<Item>));
                        Items = (ObservableCollection<Item>)serializer.ReadObject(stream);
                }
            }
            if (Items != null)
            {
                Source = new System.Windows.Data.CollectionViewSource();
                Source.Source = Items;
                Source.SortDescriptions.Add(new SortDescription("Score", ListSortDirection.Descending));
            }

            InitializeComponent();
            DataContext = this;
      }

Items ObservableCollection 包含名称和分数数据。

我尝试使用正常的 while 循环来添加数字但没有成功。而且我也无法让 AlternationCount 工作。 wp7甚至支持吗?有什么想法吗?

谢谢!

【问题讨论】:

    标签: c# xaml windows-phone-7 listbox


    【解决方案1】:

    使用 Select 扩展来进行计数并绑定一个新类,这可以通过使用行号创建一个备用列表来完成。方法如下:

    创建一个扩展的 Items 类 (ItemsEx),它有一个额外的属性,即 int for RowNumber 属性。还有一个复制构造函数,它接受 Item 并将有效信息复制到克隆中。

    拥有 ItemsEx(s) 的可观察集合并存储旧的(如果需要在列表中):

    public List<Item> Items { get; set; }                       // Original
    public ObservableCollection<ItemEx> Items2 { get; set; }    // Changed to hold the RowNumber
    

    当您拥有来自隔离存储的项目时,创建您的 ItemsEx 实例,例如

        var itemsToBePlaceInCollection 
            = Items.Select((itm, index) => new ItemEx(itm) { RowNumber = index + 1; })
                   .ToList();
                   .ForEach(itmEx => Items2.Add( itmEx ));   // Add into the observable collection at this point
    

    然后在 Xaml 中,DataContext 设置为 Items2,模板绑定到 ItemEx 之外的 RowNumber,这将反映存储中的计数,并显示您的行号。


    坦率地说,如果没有动态添加列表,为什么需要ObservableCollection 不清楚。如果不是,那么简单地使用 INotifyPropertyChanged 动态创建一个新列表将与使用 ObservableCollection 一样有效。

    【讨论】:

    • 感谢您的回复。我喜欢这个想法,用数字制作一个新列表,但就我而言,我不确定我是否能够这样做。好吧,我正在使用 observableCollection,因为我需要按分数对列表进行排序,然后我需要添加那些“行号”。事实上,我在两个不同的列表框中使用相同的文件/列表,按播放器级别(简单,正常)排序。另一个列表框包括“简单”级别分数和另一个“正常”级别分数。但是让我们保持简单 :) 也许我可以打开数据,对其进行排序和过滤,然后将其与行号放在两个不同的列表中。我会尝试!谢谢!
    • @Arnoho 如果没有其他方法,则为您的数据使用扩展类来满足视图的不同需求是一个不错的起点。
    • 对不起,我不明白你的意思?

      解决我的问题的最简单方法是为每个 LISTBOX 项添加数字 1,2,3,4,5...,n,而不是我的数据项。
      数据项按玩家得分排序,在加载高分页面时,它们会移动到列表框。这就是为什么在系统将列表框项加载到 xaml 时需要提供此编号的原因。
      我真的不知道如何解释这个,但我不需要将这个数字保存到数据中。唯一的目的是在列表框中显示它。
      所以列表看起来像:
      1. player 200p
      2. player2 10p
      3. player3 0p
      ... 以此类推。
    • @Arnoho 扩展类(基于原始类)将保存该 临时 编号,并仅允许 UI 根据需要显示该编号。之后将不会持久化该值或扩展类的使用。该类仅用于处理这种(或可能)需要扩展数据而不是原始数据一部分的其他情况。
    猜你喜欢
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多