Hej,
我得到了一个绑定到ObservableCollection的ListBox,现在我正试图实现一个搜索/筛选函数。但它不起作用。。。什么都试过了:(
这是我的列表框的图片https://i.stack.imgur.com/SuNZS.png
好的,多亏了Maximus的链接,我得到了解决方案。我更新了我的代码
这就是我迄今为止尝试的.xaml代码
<ListBox Name="lstWarning" Margin="-14,3,-31,-30">
<ListBox.ItemTemplate>
<DataTemplate>
<Canvas Height="62" Width="582">
<Label Foreground="#FFA8A4A4" FontFamily="{DynamicResource HeaderFontFamily}" FontSize="11" Content="{Binding DirName}" Canvas.Left="39" Canvas.Top="23"/>
<Label Foreground="#FFA8A4A4" FontFamily="{DynamicResource HeaderFontFamily}" FontSize="11" Content="{Binding CreationDate}" Canvas.Left="39" Canvas.Top="40"/>
<Label Foreground="White" FontFamily="{DynamicResource HeaderFontFamily}" FontSize="14" Content="{Binding FileName}" Canvas.Left="39" Canvas.Top="4"/>
<Label Foreground="#FFA8A4A4" FontFamily="{DynamicResource HeaderFontFamily}" FontSize="11" Content="{Binding Extension}" Canvas.Left="224" Canvas.Top="40"/>
<Label Foreground="#FFA8A4A4" FontFamily="{DynamicResource HeaderFontFamily}" FontSize="11" Content="{Binding FileSize}" Canvas.Left="155" Canvas.Top="40"/>
<Image Source="{Binding StatusImage}" Width="30" Height="30" Canvas.Left="10" Canvas.Top="6" Stretch="Fill"/>
</Canvas>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
下面是如果在TextChanged事件中执行的操作
private void cmdSearchWarnings_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e) {
CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(lstWarning.ItemsSource);
if (!string.IsNullOrEmpty(txtSearchWarnings.Text)) {
if (isFilter) {
cv.Filter = null;
isFilter = false;
}
else {
cv.Filter = new Predicate<object>(FilterByFileName);
isFilter = true;
}
}
else {
cv.Filter = null;
isFilter = false;
}
}
private bool FilterByFileName(object _warningObj) {
if (_warningList != null) {
if (!string.IsNullOrEmpty(txtSearchWarnings.Text)) {
var warning = _warningObj as WarningItem;
return warning.FileName.Trim().Contains(txtSearchWarnings.Text);
}
}
return false;
}
这是我的警告项目类别代码:
public class WarningItem
{
public string FullPath { get; set; }
public string DirName { get; set; }
public string FileName { get; set; }
public string FileSize { get; set; }
public string CreationDate { get; set; }
public string Extension { get; set; }
public Uri StatusImage { get; set; }
}
我正在粘贴一个简单的例子,但类似的例子已经被覆盖了无数次,你需要深入研究SO。下面的例子检查集合Names是否包含元素,而元素又包含搜索词。
private ObservableCollection<string> _names = new ObservableCollection<string>()
{
"Isabel", "Michal"
};
public ObservableCollection<string> Names
{
get { return _names; }
set { _names = value; }
}
private ICollectionView View;
public MainWindow()
{
InitializeComponent();
ListBox.ItemsSource = Names;
View = CollectionViewSource.GetDefaultView(Names);
}
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
{
View.Filter = x => x.ToString().ToLower().Contains(((TextBox)sender).Text.ToLower());
}
不需要在TextBoxChanged事件中调用CollectionViewSource.GetDefaultView(Names(,因为collectionView只检索一次并保持引用。看看这里
如果涉及到MVVM模式,您不应该使用so后面的代码而不是
<TextBox TextChanged="TextBoxBase_OnTextChanged"/>
你应该有
<TextBox Text="{Binding FilterText}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<i:InvokeCommandAction Command="{Binding FilterListCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
RelayCommand