我有ListBox
根据文本框中输入的文字过滤项目(按下输入时):
<TextBox DockPanel.Dock="Top" Margin="0,0,0,20" Width="200" HorizontalAlignment="Left" Text="{Binding Path=FilterText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Command="{Binding Path=FilterSearchCommand}" Key="Enter" />
</TextBox.InputBindings>
</TextBox>
<ListBox DockPanel.Dock="Bottom" Name="lbItems" ItemsSource="{Binding Path=MyList, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="{Binding ActualWidth, ElementName=lbItems}" Cursor="Hand" Margin="10,10,0,10" VerticalAlignment="Center" MouseLeftButtonUp="UIElement_OnMouseLeftButtonUp">
<TextBlock Text="{Binding Path=Title}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在文本框中按下ENTER时,将执行以下命令:
FilterSearchCommand = new RelayCommand(() => {
MyList = new ObservableCollection < MyObject > (MyList.Where(x => x.Title.IndexOf(FilterText, StringComparison.InvariantCultureIgnoreCase) >= 0).ToList());
});
public RelayCommand FilterSearchCommand {
get;
}
public string FilterText {
get;
set;
}
public ObservableCollection < MyObject > MyList {
get;
set;
}
基本上在输入命令时,ObservableCollection已成功更新,但列表框中的项目保持不变。
有什么想法吗?
答案 0 :(得分:1)
这里你会遇到一些问题 - 搜索后,你将覆盖你的'MyList'对象。因此,一旦过滤,就无法取消过滤。您应该考虑使用CollectionViewSource。
答案 1 :(得分:0)
您需要实现INotifyPropertyChanged,并且在MyList的setter中,您将通知UI该属性已更改。这是一个例子:
class MyViewModel : INotifyPropertyChanged
{
private ObservableCollection<MyObject> _myList;
public ObservableCollection<MyObject> MyList
{
get { return _myList; }
set
{
_myList = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}