C# 通用Windows平台下的GridView排序

C# 通用Windows平台下的GridView排序,c#,gridview,win-universal-app,windows-10-universal,uwp-xaml,C#,Gridview,Win Universal App,Windows 10 Universal,Uwp Xaml,我想对GridView中的项目进行排序 我的GridView看起来像: <GridView x:Name="ivGridView" Margin="70,40,10,10" SelectionChanged="ivGridView_SelectionChanged"> <GridView.ItemTemplate> <DataTemplate> <StackPanel Margin=

我想对GridView中的项目进行排序

我的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>


如何对它们进行排序?

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

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

除了@Filip的想法,我还将向您展示如何在GridView中对项目进行排序的详细信息

例如,如果要根据绑定到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; }
}
之后,我们可以使用ObservableCollection方法按照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;
    }
公共ObservableCollection TestOC=新ObservableCollection(); 公共主页() { this.InitializeComponent(); Add(newtest(){wpn=“BB”,sname=“BBB”,Image=new-BitmapImage(新Uri(“ms”)-appx:///Capture.PNG“”),颜色=新的SolidColorBrush(Colors.Red)}; Add(newtest(){wpn=“CC”,sname=“CCC”,Image=new-BitmapImage(新Uri(“ms”)-appx:///Capture.PNG“”),颜色=新的SolidColorBrush(Colors.Green)}; Add(newtest(){wpn=“AA”,sname=“AA”,Image=new-BitmapImage(新Uri(“ms”)-appx:///Capture.PNG“”),颜色=新的SolidColorBrush(Colors.Orange)}; var SortResult=TestOC.OrderBy(a=>a.wpn); ivGridView.ItemsSource=SortResult; }
结果是:


谢谢。

除了@Filip的想法,我将向您展示如何在GridView中对项目进行排序的详细信息

例如,如果要根据绑定到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; }
}
之后,我们可以使用ObservableCollection方法按照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;
    }
公共ObservableCollection TestOC=新ObservableCollection(); 公共主页() { this.InitializeComponent(); Add(newtest(){wpn=“BB”,sname=“BBB”,Image=new-BitmapImage(新Uri(“ms”)-appx:///Capture.PNG“”),颜色=新的SolidColorBrush(Colors.Red)}; Add(newtest(){wpn=“CC”,sname=“CCC”,Image=new-BitmapImage(新Uri(“ms”)-appx:///Capture.PNG“”),颜色=新的SolidColorBrush(Colors.Green)}; Add(newtest(){wpn=“AA”,sname=“AA”,Image=new-BitmapImage(新Uri(“ms”)-appx:///Capture.PNG“”),颜色=新的SolidColorBrush(Colors.Orange)}; var SortResult=TestOC.OrderBy(a=>a.wpn); ivGridView.ItemsSource=SortResult; }
结果是:


谢谢。

与列表不同,可能更好的选择是ObservableCollection,它允许在任何时候对项目进行排序,而不仅仅是在将其设置为ItemsSource之前。是的,你是对的。使用ObservableCollection可能是一个更好的选择。我已经更新了我的答案。:),谢谢。这并没有说明如何在不必继续设置
项资源的情况下对
可观察集合进行排序,这通常是不理想的+1如果可以更彻底地回答这个问题。与列表相比,可能更好的选择是ObservableCollection,它允许在任何时候对项目进行排序,而不仅仅是在将其设置为ItemsSource之前。是的,您是对的。使用ObservableCollection可能是一个更好的选择。我已经更新了我的答案。:),谢谢。这并没有说明如何在不必继续设置
项资源的情况下对
可观察集合进行排序,这通常是不理想的+1如果这个问题能得到更彻底的回答。