开发者

How do I use Comparer to sort a List<T> when I have multiple parameters to choose from?

开发者 https://www.devze.com 2023-03-11 14:11 出处:网络
and thanks for looking. I\'m trying the extend the example I found here. My problem is this: The list in the example contains countries: each country has a name, flag, description, capital and ID. I

and thanks for looking. I'm trying the extend the example I found here. My problem is this:

The list in the example contains countries: each country has a name, flag, description, capital and ID. I would like to use the appbar menu, to sort the list on the page according to whatever parameter I choose. XAML would look like so:

<shell:ApplicationBar.MenuItems>
      <shell:ApplicationBarMenuItem Text="sort by capital..." Click="SortCapital_Click"/>
      <shell:ApplicationBarMenuItem Text="sort by ID..." Click="SortID_Click"/>
</shell:ApplicationBar.MenuItems>

In order to separate the different sort-by-parameter scenarios from the default read-from-XML order, I've had to create some extra variables and overloads, and override the OnNavigatedTo event:

public interface ICountryRepository
{
    IList<CountryData> GetCountryList(); // called by constructor
    IList<CountryData> GetCountryList(string sortMode); // called when sort is clicked
    CountryData GetCountryById(int id);
}

开发者_如何学编程...

public class XmlCountryRepository : ICountryRepository
{
private static List<CountryData> countryList = null;


    static XmlCountryRepository()
    {
        XDocument loadedData = XDocument.Load("CountriesXML.xml");

        var data = from query in loadedData.Descendants("Country")
          select new CountryData
          {
              Name = (string)query.Element("Name"),
              Flag = (string)query.Element("Flag"),
              Description = (string)query.Element("Description"),
              Capital = (string)query.Element("Capital"),
              ID = (int)query.Element("ID"),
          };
        countryList = data.ToList();
    }

    public IList<CountryData> GetCountryList()
    {
        return countryList;
    }

    public IList<CountryData> GetCountryList(string sortMode)
    {
        switch (sortMode)
        {
            case "capital": // Sort by capital
                break;
            case "ID": // Sort by ID
                    break;
        }
        return countryList;
    }

and finally in the main class, the constructor calls GetCountryList(), which reads the items in the list in the order specified in the original XML file. My current implementation uses the click handlers of the app menu to set flags and navigate to the page again, but there has to be a better way to do this!

    private void SortID_Click(object sender, System.EventArgs e)
    {
        sortMode = "ID";
        sortModeChanged = true;
        NavigationService.Navigate(new Uri("List.xaml", UriKind.Relative));
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        if (sortModeChanged)
        {
            ICountryRepository countryRepository = new XmlCountryRepository();
            this.list.ItemsSource = countryRepository.GetCountryList(sortMode);
            sortModeChanged = false;
        }
    }

I tried looking at the sort example on MSDN, but I couldn't figure out how to extend it to more than one parameter of different types (int, string, etc)


You should bind your ListBox to a CollectionViewSource with a ObservableCollection as the Source. The CVS will be used as proxy and on it you can have grouping and sorting capabilities. Here's a sample:

http://weblogs.asp.net/psheriff/archive/2010/07/22/sort-data-using-code-with-the-silverlight-collectionviewsource.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号