谢谢你的期待。我正在尝试扩展我找到的here示例。我的问题是:
示例中的列表包含国家/地区:每个国家/地区都有名称,标记,说明,资本和ID。我想使用appbar菜单,根据我选择的任何参数对页面上的列表进行排序。 XAML看起来像这样:
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="sort by capital..." Click="SortCapital_Click"/>
<shell:ApplicationBarMenuItem Text="sort by ID..." Click="SortID_Click"/>
</shell:ApplicationBar.MenuItems>
为了将不同的逐个参数场景与默认的read-from-XML顺序分开,我不得不创建一些额外的变量和重载,并覆盖OnNavigatedTo事件:
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;
}
最后在主类中,构造函数调用GetCountryList(),它按原始XML文件中指定的顺序读取列表中的项。我当前的实现使用应用程序菜单的单击处理程序来设置标志并再次导航到页面,但必须有更好的方法来执行此操作!
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;
}
}
我尝试查看sort example on MSDN,但我无法弄清楚如何将其扩展为多个不同类型的参数(int,string等)
答案 0 :(得分:0)
您应该将ListBox绑定到CollectionViewSource,并将ObservableCollection作为Source。 CVS将用作代理,在其上您可以拥有分组和排序功能。这是一个示例: