c# - 基于文本框值的列表框排序

标签 c# wpf mvvm

如何使用TextBox输入的值对ListBox项目进行排序。
火柴盒列表框项目显示在列表框的顶部
这是我的代码

XAML

<Grid>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="67,39,0,0" TextWrapping="Wrap" Text="{Binding CustomerSort,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
    <ListBox Margin="67,79,185,26" Background="LightYellow"
            DisplayMemberPath="Name" ItemsSource="{Binding CustomerColloction}"/>
</Grid>

C#代码
 public class Customers
{
    public String Name { get; set; }
    public Int32 Age { get; set; }
}

public class ViewModel
{
    public ViewModel()
    {
        CustomerColloction = new ObservableCollection<Customers>();
        CustomerColloction.Add(new Customers() { Name = "AAA", Age = 25 });
        CustomerColloction.Add(new Customers() { Name = "AA", Age = 25 });
        CustomerColloction.Add(new Customers() { Name = "BBB", Age = 26 });
        CustomerColloction.Add(new Customers() { Name = "BB", Age = 24 });
        CustomerColloction.Add(new Customers() { Name = "AAAA", Age = 13 });
        CustomerColloction.Add(new Customers() { Name = "BB", Age = 11 });
        CustomerColloction.Add(new Customers() { Name = "XYZ", Age = 44 });
    }
    public ObservableCollection<Customers> CustomerColloction { get; set; }

    private String _CustomerSort = String.Empty;
    public String CustomerSort
    {
        get
        {
            return _CustomerSort;
        }
        set
       {
            _CustomerSort = value;
            NotifyPropertyChanged("CustomerSort");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

我想得到的结果就像我们在文本框,显示排序项目表单列表框中输入AA一样
  • AA
  • AAA
  • AAAA
  • BB
  • BB
  • BBB
  • XYZ
  • 最佳答案

    设置组合框并设置下拉模式
    最好对可观察的集合进行排序,并将其设置为ListBox CustomerColloction.OrderBy(q => q.Name).ToList();

    关于c# - 基于文本框值的列表框排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29308903/

    相关文章:

    c# - 如何在Xaml中为使用mvvm代码创建的窗口添加样式?

    C#:WPF MVVM 命令绑定(bind)与事件回调

    c# - CYK (Cocke-Younger-Kasami) 文法规则

    c# - 无法检索连接 MVC3 EF 与 Postgres 9.1 的元数据

    c# - 在线程中执行 Webbrowser 控件的屏幕显示

    c# - 如何在不使用设置应用程序的情况下在 Windows 10 平板电脑应用程序中发现未配对的蓝牙设备

    WPF DataGrid,添加行时应用程序崩溃

    c# - 当鼠标悬停在按钮WPF上时,如何制作更改图像的图像按钮?

    c# - 了解 OpacityMask 的有效区域

    javascript - MVVM Javascript 与传统 ASP.NET MVC 网站的可扩展性比较如何