如何使用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一样
最佳答案
设置组合框并设置下拉模式
最好对可观察的集合进行排序,并将其设置为ListBox CustomerColloction.OrderBy(q => q.Name).ToList();
关于c# - 基于文本框值的列表框排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29308903/