Hej,
我有一个ListBox绑定到ObservableCollection,现在我正在尝试实现搜索/过滤功能。但它不起作用......尝试了一切:(
下载我的ListBox的图片http://i.imgur.com/el8KF3T.png
Ok得到了解决方案,感谢Maximus的Link。我已经更新了我的代码。
并且我到目前为止尝试了我的.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;
}
继承我的WarningItem类代码:
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; }
}
答案 0 :(得分:0)
我正在粘贴一个简单的例子,但类似的内容涵盖了无数次你需要深入研究SO。以下示例检查集合名称是否包含搜索单词的元素。
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被检索一次并保持引用。看看here。
如果涉及MVVM模式,则不应使用代码隐藏而不是
<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>