【问题标题】:How can I sort a XAML gridview in a Windows 8.1 app?如何在 Windows 8.1 应用程序中对 XAML 网格视图进行排序?
【发布时间】: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 的排序?

【问题讨论】:

    标签: c# xaml windows-store-apps windows-8.1


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 2015-03-11
      相关资源
      最近更新 更多