locked
Different sorting in ListView RRS feed

  • Question

  • Hi!

    I have got an app with two pivot pages that displays a list of flowernames in Norwegian and in Latin. How do I get to sort the ListViews in the two PivotItems differently? In the first PivotItem I would like to sort by the name column and in the second PivotItem I would like to sort by the Subtitle column.
    Thanks, Sigurd F

    <Pivot x:Uid="AppTitle" Title="*Flores" >
                <PivotItem x:Uid="PivotNorwegian" Header="*Norsk namn" DataContext="{Binding Groups}">
                    <ListView
                        ItemsSource="{Binding}"
                        IsItemClickEnabled="True"
                        ItemClick="GroupSection_ItemClick"
                        ContinuumNavigationTransitionInfo.ExitElementContainer="True">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Margin="0,0,0,5">
                                    <TextBlock Text="{Binding Title}" Style="{ThemeResource ListViewItemTextBlockStyle}"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </PivotItem>
    
                <PivotItem x:Uid="PivotLatin" Header="*Latinsk namn" DataContext="{Binding Groups}">
                    <ListView
                        ItemsSource="{Binding}"
                        IsItemClickEnabled="True"
                        ItemClick="GroupSection_ItemClick"
                        ContinuumNavigationTransitionInfo.ExitElementContainer="True">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Margin="0,0,0,5">
                                    <TextBlock Text="{Binding Subtitle}" Style="{ThemeResource ListViewItemTextBlockStyle}" />
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </PivotItem>

    Saturday, April 18, 2015 11:06 PM

Answers

  • Hi Sigurd F,

    Based on your description, I can see that both of your two ListViews have bound to a same DataContext in the xaml, then I will recommend you do the binding in the .cs code to let sort the ListView become easily. The following is a simple example about how to sort the two ListViews by the first letter of the name column or Subtitle column, please try to check it:

    First I have defined the following class for data binding:

     public class test
        {
            public string name { get; set; }
            public string Title { get; set; }
            public string Subtitle { get; set; }
        }
    Aftet that we can use the List OrderBy method to sort the ListView by the first letter of the name column or Subtitle column:
    List<test> MyList = new List<test>();
    MyList.Add(new test() { name = "aa", Title = "vv", Subtitle = "rr" });
    MyList.Add(new test() { name = "bb", Title = "dd", Subtitle = "aa" });

    List<test> MyList1 = MyList.OrderBy<test, string>(Item => Item.name.Substring(0)).ToList();
    MyListView1.ItemsSource = MyList1;

    List<test> MyList2 = MyList.OrderBy<test, string>(Item => Item.Subtitle.Substring(0)).ToList();
    MyListView2.ItemsSource = MyList2;

    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Monday, April 20, 2015 9:45 AM
    Moderator

All replies

  • Hi Sigurd F,

    Based on your description, I can see that both of your two ListViews have bound to a same DataContext in the xaml, then I will recommend you do the binding in the .cs code to let sort the ListView become easily. The following is a simple example about how to sort the two ListViews by the first letter of the name column or Subtitle column, please try to check it:

    First I have defined the following class for data binding:

     public class test
        {
            public string name { get; set; }
            public string Title { get; set; }
            public string Subtitle { get; set; }
        }
    Aftet that we can use the List OrderBy method to sort the ListView by the first letter of the name column or Subtitle column:
    List<test> MyList = new List<test>();
    MyList.Add(new test() { name = "aa", Title = "vv", Subtitle = "rr" });
    MyList.Add(new test() { name = "bb", Title = "dd", Subtitle = "aa" });

    List<test> MyList1 = MyList.OrderBy<test, string>(Item => Item.name.Substring(0)).ToList();
    MyListView1.ItemsSource = MyList1;

    List<test> MyList2 = MyList.OrderBy<test, string>(Item => Item.Subtitle.Substring(0)).ToList();
    MyListView2.ItemsSource = MyList2;

    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Monday, April 20, 2015 9:45 AM
    Moderator
  • Thanks a lot for the help! Now I got one ordered list. But where do I put this code to also sort the list With the latin flower names? Is there an event for the PivotItem for detecting when the user has changed PivotItem? I guess I need a little more help to get this going :-)

    Thanks, Sigurd F

    Tuesday, April 21, 2015 8:17 PM
  • I think I found a way to solve this, but I'm not sure if it is a good solution? Is there a better way to do this?

    Thanks, Sigurd F

    <Pivot x:Uid="AppTitle" Title="*Flores" SelectionChanged="AppTitle_SelectionChanged">
    
    --------------------------
    
            private async void AppTitle_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                Pivot myPivot = sender as Pivot;
                if (myPivot.SelectedIndex == 0)
                { //norwegian names
                    var sampleDataGroups = await SampleDataSource.GetGroupsAsync();
                    this.DefaultViewModel["Groups"] = sampleDataGroups.OrderBy<SampleDataGroup, string>(Item => Item.Title.Substring(0)).ToList();
                    MyListView1.ItemsSource = DefaultViewModel["Groups"];
                }
                else if (myPivot.SelectedIndex == 1)
                { // latin names
                    var sampleDataGroups = await SampleDataSource.GetGroupsAsync();
                    this.DefaultViewModel["Groups"] = sampleDataGroups.OrderBy<SampleDataGroup, string>(Item => Item.Subtitle.Substring(0)).ToList();
                    MyListView1.ItemsSource = DefaultViewModel["Groups"];
                }
            }
    

    Tuesday, April 21, 2015 9:42 PM