gpt4 book ai didi

c# - WPF 过滤器 ObservableCollection 与 ICollectionView

转载 作者:行者123 更新时间:2023-11-30 18:18:34 24 4
gpt4 key购买 nike

我有一个 ObservableCollection、一个 ICollectionView、一个过滤器和两个 TextBox。如果我只为一个 TextBox 使用过滤器,它就可以正常工作。如果我添加另一个文本框并将过滤器绑定(bind)到第二个 TB,过滤就会很困难。

我已经用两个过滤器(不同的名称 - 相同的功能)试过了,这也没有用。

我认为它可能与 ObservableCollection 相关..

这是我的过滤器:

this.AllUsers.Filter = i =>
{
if (string.IsNullOrEmpty(this.SearchUsername)) return true;

User u = i as User;
return u.Name.StartsWith(this.SearchUsername);
};

我的 ICollectionView 包含来自 ObservableCollection 的数据:

public ICollectionView AllUsers
{
get
{
return CollectionViewSource.GetDefaultView(UserSource);
}
}

还有我的 ObservableCollection:

public ObservableCollection<User> UserSource
{
get
{
return _UserSource;
}
set
{
_UserSource = value; OnPropertyChanged();
}

}

我正在使用字符串属性 SearchUsername 中的 AllUsers.Refresh(); 更新 View 。

ObservableCollection 绑定(bind)到 ListBox,字符串属性绑定(bind)到 TextBox。

第二个文本框也一样。同一个 ObservableCollection 绑定(bind)到不同的 ListBox,字符串属性(UserName)绑定(bind)到第二个 TextBox。

那么有什么简单的方法可以解决这个问题吗?

最佳答案

这里是一个使用 Collection View Source 的 duel 过滤器的例子

查看

<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>

型号:

public class DataModel
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}

查看模型:(使用 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>();
}

关于c# - WPF 过滤器 ObservableCollection 与 ICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41120344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com