C# 如何在Windows 8.1应用程序中对XAML gridview排序?

C# 如何在Windows 8.1应用程序中对XAML gridview排序?,c#,xaml,windows-store-apps,windows-8.1,C#,Xaml,Windows Store Apps,Windows 8.1,关于GridView,我有以下xaml代码: <GridView x:Name="ivGridView" Margin="70,40,10,10" SelectionChanged="ivGridView_SelectionChanged"> <GridView.ItemTemplate> <DataTemplate> <StackPanel Background="{Binding Color}"

关于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>


如何基于绑定到name的Textblock值实现GridView的排序?

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

 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;
}
公共ObservableCollection TestOC=新ObservableCollection(); 公共主页() { this.InitializeComponent(); Add(newtest(){name=“BBB”,id=“1”,Color=newsolidColorBrush(Colors.Red)}); Add(newtest(){name=“CCC”,id=“11”,Color=newsolidColorBrush(Colors.Green)}); Add(newtest(){name=“AA”,id=“111”,Color=newsolidColorBrush(Colors.Orange)}); var SortResult=TestOC.OrderBy(a=>a.name); ivGridView.ItemsSource=SortResult; }
您可以对相关项目进行排序Source可对视图中的项目进行排序

 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;
}
公共ObservableCollection TestOC=新ObservableCollection(); 公共主页() { this.InitializeComponent(); Add(newtest(){name=“BBB”,id=“1”,Color=newsolidColorBrush(Colors.Red)}); Add(newtest(){name=“CCC”,id=“11”,Color=newsolidColorBrush(Colors.Green)}); Add(newtest(){name=“AA”,id=“111”,Color=newsolidColorBrush(Colors.Orange)}); var SortResult=TestOC.OrderBy(a=>a.name); ivGridView.ItemsSource=SortResult; }