我有一个 ObservableCollection、一个 ICollectionView、一个过滤器和两个文本框。如果我仅对一个文本框使用过滤器,则效果很好。如果我添加另一个文本框并将过滤器绑定到第二个 TB,则过滤会很困难。
我用两个过滤器(不同的名称 - 相同的功能)尝试过,但这也不起作用。
我认为这可能与 ObservableCollection 有关。
这是我的过滤器:
this.AllUsers.Filter = i =>
{
if (string.IsNullOrEmpty(this.SearchUsername)) return true;
User u = i as User;
return u.Name.StartsWith(this.SearchUsername);
};
Run Code Online (Sandbox Code Playgroud)
我的 ICollectionView 包含来自 ObservableCollection 的数据:
public ICollectionView AllUsers
{
get
{
return CollectionViewSource.GetDefaultView(UserSource);
}
}
Run Code Online (Sandbox Code Playgroud)
和我的 ObservableCollection:
public ObservableCollection<User> UserSource
{
get
{
return _UserSource;
}
set
{
_UserSource = value; OnPropertyChanged();
}
}
Run Code Online (Sandbox Code Playgroud)
AllUsers.Refresh();我正在使用字符串属性 SearchUsername更新视图。
ObservableCollection 绑定到 ListBox,字符串 Property 绑定到 TextBox。
第二个文本框也是如此。相同的 ObservableCollection 绑定到不同的 ListBox,字符串属性(UserName)绑定到第二个 TextBox。
那么有没有一种简单的方法可以解决这个问题呢?
这是使用集合视图源的双重过滤器的示例
看法
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:ViewModel x:Name="vm" />
</Window.DataContext>
<DockPanel >
<TextBox DockPanel.Dock="Top" Text="{Binding Search1}"/>
<TextBox DockPanel.Dock="Top" Text="{Binding Search2}"/>
<ListView ItemsSource="{Binding View}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Property1}" Header="Prop1"/>
<GridViewColumn DisplayMemberBinding="{Binding Property2}" Header="Prop1"/>
</GridView>
</ListView.View>
</ListView>
</DockPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
模型:
public class DataModel
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
查看模型:(使用 Prism 和 c#6)
public class ViewModel:BindableBase
{
public ViewModel()
{
for (int i = 0; i < 200; i++)
{
Items.Add(new DataModel()
{
Property1 = $"{200 - i}:Prop1",
Property2 = $"{i}:Prop2"
});
}
//add filter
CollectionViewSource.Filter += (s, e) =>
{
var d = e.Item as DataModel;
if (d != null)
{
e.Accepted = (string.IsNullOrEmpty(Search1) || d.Property1.StartsWith(Search1))//search property 1
&& (string.IsNullOrEmpty(Search2) || d.Property2.StartsWith(Search2));//search property 2
}
else
e.Accepted = false;
};
CollectionViewSource.Source = Items;
}
public CollectionViewSource CollectionViewSource { get; } = new CollectionViewSource();
public ICollectionView View => CollectionViewSource.View;
private string _Search1;
public string Search1
{
get { return _Search1; }
set
{
if (SetProperty(ref _Search1, value))
View.Refresh();
}
}
private string _Search2;
public string Search2
{
get { return _Search2; }
set
{
//SetProperty defined as if value is different update, raise PropertyChanged and return true, else return false;
if (SetProperty(ref _Search2, value))
View.Refresh();
}
}
public ObservableCollection<DataModel> Items { get; } = new ObservableCollection<DataModel>();
}
Run Code Online (Sandbox Code Playgroud)