【问题标题】:Uwp Autosuggestbox OrderBy DisplaymemberpathUwp Autosuggestbox OrderBy Displaymemberpath
【发布时间】:2020-10-02 11:25:23
【问题描述】:

我的 autosuggestbox 有一个行为,我必须按升序对所有建议的列表项进行排序,并且此行为将应用于整个应用程序中的一种常见 AutoSuggestBox 样式。当我尝试简单地使用对象本身进行排序时,它工作得很好,因为这些项目只是一个字符串列表。但是,当项目是对象列表,并且我想使用 1 个特定属性进行排序时,它对我不起作用。我正在使用 DisplayMemberPath 告诉它应该查找哪个属性。下面是我尝试过的代码:

行为

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;
}

Xaml

<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" />

TextChanged 事件

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;
    }

【问题讨论】:

    标签: c# xaml uwp sql-order-by textchanged


    【解决方案1】:

    所以我通过一个小实验找到了解决方案,我们可以使用 GetValue() 方法并传入对象本身,然后它按预期工作:

    autoSuggestBox.ItemsSource = autoSuggestBox.Items.ToList().OrderBy(x => x.GetType().GetProperty(autoSuggestBox.DisplayMemberPath).GetValue(x)).ToList();
    

    【讨论】:

      猜你喜欢
      • 2017-11-24
      • 2018-01-20
      • 2017-07-27
      • 2015-11-29
      • 2016-04-09
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多