为什么我的 ObservableCollection 不过滤

why does my ObservableCollection not filter

我有一个 WPF 桌面应用程序。

我加载了一个包含对象列表的 ObservableCollection。

我使用 ICollectionView 对象'wrap'围绕这个 ObservableCollection 的过滤器。

我设置了过滤器并刷新,但它不起作用,所以:

public class DataFilters : ViewModelBase
{
    private ICollectionView _UserMappedRolesView { get; set; }

    private ObservableCollection<UserMappedRoles> _UserMappedRoles;
    public ObservableCollection<UserMappedRoles> UserMappedRoles
    {
        get
        {
            _UserMappedRolesView = CollectionViewSource.GetDefaultView(_UserMappedRoles);
            _UserMappedRolesView.Filter = UserMappedRolesFilter;
            _UserMappedRolesView.Refresh();
            return _UserMappedRoles;
        }
        set
        {
            _UserMappedRoles = value;
        }
    }

    public void LoadUserMappedRoles()
    {
        var baseData = InformedWorkerBusinessService.UserMappedRoles.Get();
        var modelData =
            from data in baseData
            select new UserMappedRoles
            {
                Enabled = 1,
                Login = data.Login,
                UserMappedRolesRef = data.UserMappedRolesRef,
                UserRoleRef = data.UserRoleRef

            };
        _UserMappedRoles = new ObservableCollection<UserMappedRoles>(modelData);
    }
    public string Login { get; set; }
    private bool UserMappedRolesFilter(object item)
    {
        UserMappedRoles UserMappedRole = item as UserMappedRoles;

        if (UserMappedRole.Login== Login)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

和我的测试脚本:

UI.InformedWorkerViewModel.Models.HeartBeat VM = new UI.InformedWorkerViewModel.Models.HeartBeat();
VM.CommonData.DataFilters = new UI.InformedWorkerViewModel.Models.DataFilters();
VM.CommonData.DataFilters.LoadUserMappedRoles();
var data = VM.CommonData.DataFilters.UserMappedRoles;
VM.CommonData.DataFilters.Login = "David";
var filtered = VM.CommonData.DataFilters.UserMappedRoles;

我知道我的数据只包含登录名是 'Andy' 的 oUserMappedRoles,因此,通过将登录过滤器名称设置为 'David',我希望不会返回任何记录。

我到处都设置了断点,每一行代码都得到'hit'。

我(显然)执行错了吗?

谢谢

过滤的是 ICollectionView,而不是 ObservableCollection。

所以你应该绑定到 ICollectionView 属性:

<ListBox ItemsSource="{Binding UserMappedRolesView}" DisplayMemberPath="Login" />

...或在这一项中查找过滤后的项目:

var filtered = VM.CommonData.DataFilters.UserMappedRolesView;

您还需要在想要重新应用过滤器时刷新 CollectionView,即每当您的登录 属性 设置为新值时。像这样:

public class DataFilters : ViewModelBase
{
    private ICollectionView _UserMappedRolesView;
    public ICollectionView UserMappedRolesView
    {
        get { return _UserMappedRolesView; }
        set { _UserMappedRolesView = value; NotifyPropertyChanged(); }
    }

    private ObservableCollection<UserMappedRoles> _UserMappedRoles;
    public ObservableCollection<UserMappedRoles> UserMappedRoles
    {
        get
        {
            return _UserMappedRoles;
        }
        set
        {
            _UserMappedRoles = value;
            NotifyPropertyChanged();
            UserMappedRolesView = CollectionViewSource.GetDefaultView(_UserMappedRoles);
            UserMappedRolesView.Filter = UserMappedRolesFilter;
            UserMappedRolesView.Refresh();
        }
    }

    public void LoadUserMappedRoles()
    {
        var baseData = InformedWorkerBusinessService.UserMappedRoles.Get();
        var modelData =
            from data in baseData
            select new UserMappedRoles
            {
                Enabled = 1,
                Login = data.Login,
                UserMappedRolesRef = data.UserMappedRolesRef,
                UserRoleRef = data.UserRoleRef

            };
        UserMappedRoles = new ObservableCollection<UserMappedRoles>(modelData);
    }


    private string _login;
    public string Login
    {
        get { return _login; }
        set { _login = value; _UserMappedRolesView.Refresh(); }
    }


    private bool UserMappedRolesFilter(object item)
    {
        UserMappedRoles UserMappedRole = item as UserMappedRoles;
        if (UserMappedRole.Login == Login)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}