0

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

我怎样才能对它们进行排序?

4

2 回答 2

3

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

例如,您想根据已绑定到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 的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;
    }

结果:

在此处输入图像描述

谢谢。

于 2016-03-29T04:40:52.027 回答
0

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

于 2016-03-28T23:59:36.473 回答