[英]Uwp Autosuggestbox OrderBy Displaymemberpath
I have a behavior for my autosuggestbox where I have to order all the suggested list items by ascending order, and this behavior will is being applied to 1 common style of AutoSuggestBox across whole app.我的 autosuggestbox 有一个行为,我必须按升序对所有建议的列表项进行排序,并且此行为将应用于整个应用程序中 AutoSuggestBox 的 1 种常见样式。 When I try ordering simply with the object itself it works fine as the items are just a list of strings.
当我尝试简单地使用对象本身进行排序时,它工作正常,因为项目只是一个字符串列表。 But when the items are a list of objects, and I want to sort with 1 specific property, then it is not working for me.
但是当项目是对象列表时,我想用 1 个特定属性进行排序,那么它对我不起作用。 I am using DisplayMemberPath to tell it which property it should look for.
我正在使用 DisplayMemberPath 来告诉它应该寻找哪个属性。 Below is the code I tried with :
下面是我试过的代码:
public class AutoSuggestSortBehavior : IBehavior
{
public void Attach(DependencyObject associatedObject) => ((AutoSuggestBox) associatedObject).TextChanged += AutoSuggestSortBehavior_TextChanged;
private void AutoSuggestSortBehavior_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
var autoSuggestBox = sender;
if(autoSuggestBox?.Items?.Count > 0 && args.Reason == AutoSuggestionBoxTextChangeReason.UserInput && !string.IsNullOrWhiteSpace(sender.Text))
{
if (!string.IsNullOrWhiteSpace(autoSuggestBox.DisplayMemberPath))
{
autoSuggestBox.ItemsSource = autoSuggestBox.Items.ToList().OrderBy(x => x.GetType().GetProperty(autoSuggestBox.DisplayMemberPath).Name).ToList();
}
else
{
autoSuggestBox.ItemsSource = autoSuggestBox.Items.ToList().OrderBy(x => x).ToList();
}
}
}
public void Detach(DependencyObject associatedObject) => ((AutoSuggestBox) associatedObject).TextChanged -= AutoSuggestSortBehavior_TextChanged;
}
<AutoSuggestBox
Header="AutoSuggest"
QueryIcon="Find"
Text="With text, header and icon"
TextChanged="AutoSuggestBox_TextChanged" />
<AutoSuggestBox
DisplayMemberPath="Name"
Header="AutoSuggest2"
QueryIcon="Find"
Text="With text, header and icon"
TextChanged="AutoSuggestBox_TextChanged2" />
private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
var abcc = new List<string>();
abcc.Add("xyz");
abcc.Add("321");
abcc.Add("123");
abcc.Add("lopmjk");
abcc.Add("acb");
sender.ItemsSource = abcc;
}
private void AutoSuggestBox_TextChanged2(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
var persons = new List<Person>();
persons.Add(new Person { Name = "xyz", count = 1 });
persons.Add(new Person { Name = "321", count = 2 });
persons.Add(new Person { Name = "123", count = 3 });
persons.Add(new Person { Name = "lopmjk", count = 4 });
persons.Add(new Person { Name = "acb", count = 5 });
sender.ItemsSource = persons;
}
所以我通过一个小实验找到了解决方案,我们可以使用 GetValue() 方法传入对象本身,然后它按预期工作:
autoSuggestBox.ItemsSource = autoSuggestBox.Items.ToList().OrderBy(x => x.GetType().GetProperty(autoSuggestBox.DisplayMemberPath).GetValue(x)).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.