【问题标题】:Sorting GridView in Universal Windows Platform在通用 Windows 平台中对 GridView 进行排序
【发布时间】:2016-03-29 13:21:27
【问题描述】:

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

如何对它们进行排序?

【问题讨论】:

    标签: c# gridview win-universal-app windows-10-universal uwp-xaml


    【解决方案1】:

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

    例如你想根据绑定到wpn的TextBlock文本的首字母对GridView Items进行排序,那么我们需要先定义如下类进行数据绑定:

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

    之后我们可以使用ObservableCollection的OrderBy方法对ListView进行wpn列的首字母排序,并将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;
        }
    

    结果:

    谢谢。

    【讨论】:

    • 而不是 List,可能更好的选择是 ObservableCollection,它允许在任何时候对项目进行排序,而不仅仅是在将其设置为之前ItemsSource.
    • 是的,你是对的。使用 ObservableCollection 可能是更好的选择。我已经更新了我的答案。 :),谢谢。
    • 这并没有说明如何对ObservableCollection 进行排序,而不必继续设置ItemsSource,这通常理想。 +1 如果这个问题可以得到更彻底的回答。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多