简体   繁体   English

在通用Windows平台中对GridView进行排序

[英]Sorting GridView in Universal Windows Platform

I want to sort items in my GridView. 我想对GridView中的项目进行排序。

My GridView looks like: 我的GridView看起来像:

<GridView x:Name="ivGridView" Margin="70,40,10,10" SelectionChanged="ivGridView_SelectionChanged">
        <GridView.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="0,0,0,10"  Width="121" Height="128" VerticalAlignment="Top" Background="#19000000">
                    <Image Source="{Binding Image}" VerticalAlignment="Top" Height="88" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5" >
                        <Image.RenderTransform>
                            <CompositeTransform ScaleX="1.2" ScaleY="1.2"/>
                        </Image.RenderTransform>
                    </Image>
                    <StackPanel Background="{Binding Color}" HorizontalAlignment="Stretch" VerticalAlignment="Bottom">
                        <TextBlock Text="{Binding wpn}" Foreground="White" Margin="10,0,0,0" />
                        <TextBlock Text="{Binding sname}" Foreground="White" Margin="7,0,0,0" FontWeight="Light" />
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </GridView.ItemTemplate>

How can I sort them? 如何分类?

In addition to @Filip's idea, I will show you the detailed information about how to sort the Items in the GridView. 除了@Filip的想法外,我还将向您展示有关如何在GridView中对项目进行排序的详细信息。

For example you want to sort the GridView Items based on the first letter of the TextBlock text which has been binded to the wpn , then we need to define the following class for data binding first: 例如,您要基于已绑定到wpn的TextBlock文本的第一个字母对GridView项目进行排序,那么我们需要首先定义以下类用于数据绑定:

 public class Test
{
    public BitmapImage Image { get; set; }
    public SolidColorBrush Color { get; set; }
    public string wpn { get; set; }
    public string sname { get; set; }
}

After that we can use the OrderBy method of the ObservableCollection to sort the ListView by the first letter of the wpn column and bind the ObservableCollection to the ItemsSource of the GridView as following: 之后,我们可以使用ObservableCollection的OrderBy方法按wpn列的第一个字母对ListView进行排序,然后将ObservableCollection绑定到GridView的ItemsSource,如下所示:

 public ObservableCollection<Test> TestOC = new ObservableCollection<Test>();
    public MainPage()
    {
        this.InitializeComponent();
        TestOC.Add(new Test() {wpn="BB",sname="BBB",Image = new BitmapImage(new Uri("ms-appx:///Capture.PNG")),Color=new SolidColorBrush(Colors.Red)});
        TestOC.Add(new Test() { wpn = "CC", sname = "CCC", Image = new BitmapImage(new Uri("ms-appx:///Capture.PNG")), Color = new SolidColorBrush(Colors.Green) });
        TestOC.Add(new Test() { wpn = "AA", sname = "AA", Image = new BitmapImage(new Uri("ms-appx:///Capture.PNG")), Color = new SolidColorBrush(Colors.Orange) });
        var SortResult = TestOC.OrderBy(a => a.wpn);           
        ivGridView.ItemsSource =SortResult;
    }

The result: 结果:

在此处输入图片说明

Thanks. 谢谢。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM