如何在 Windows 8.1 应用程序中对 XAML 网格视图进行排序?

Posted

技术标签:

【中文标题】如何在 Windows 8.1 应用程序中对 XAML 网格视图进行排序?【英文标题】:How can I sort a XAML gridview in a Windows 8.1 app? 【发布时间】:2017-02-09 04:06:10 【问题描述】:

我有以下关于 GridView 的 xaml 代码:

<GridView x:Name="ivGridView" Margin="70,40,10,10" SelectionChanged="ivGridView_SelectionChanged">
    <GridView.ItemTemplate>
        <DataTemplate>
                <StackPanel Background="Binding Color" HorizontalAlignment="Stretch" VerticalAlignment="Bottom">
                    <TextBlock Text="Binding name" Foreground="White" Margin="10,0,0,0" />
                    <TextBlock Text="Binding id" Foreground="White" Margin="7,0,0,0" FontWeight="Light" />
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </GridView.ItemTemplate>

如何根据绑定到名称的 Textblock 值实现 GridView 的排序?

【问题讨论】:

【参考方案1】:

您可以对关联的 ItemsSource 进行排序以对视图中的项目进行排序。

 public ObservableCollection<Test> TestOC = new ObservableCollection<Test>();
public MainPage()

    this.InitializeComponent();
    TestOC.Add(new Test() name="BBB",id="1",Color=new SolidColorBrush(Colors.Red));
    TestOC.Add(new Test()  name = "CCC", id="11", Color = new SolidColorBrush(Colors.Green) );
    TestOC.Add(new Test()   name = "AA", id="111", Color = new SolidColorBrush(Colors.Orange) );
    var SortResult = TestOC.OrderBy(a => a.name);           
    ivGridView.ItemsSource =SortResult;

【讨论】:

以上是关于如何在 Windows 8.1 应用程序中对 XAML 网格视图进行排序?的主要内容,如果未能解决你的问题,请参考以下文章