为什么我的ObservableCollection没有过滤

时间:2016-12-23 10:22:54

标签: wpf observablecollection icollectionview

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

我加载一个带有对象列表的ObservableCollection。

我使用ICollectionView对象来包装'围绕此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;

我知道我的数据只包含oUserMappedRoles,登录名是&#39; Andy&#39;所以,通过将登录过滤器名称设置为&#39; David&#39;我希望没有记录回来。

我已经在任何地方设置了断点,代码的每一行都被点击了

我(显然)实施了这个错误吗?

由于

1 个答案:

答案 0 :(得分:1)

是过滤的ICollectionView,而不是ObservableCollection。

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

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

...或者在这个中查找过滤的项目:

var filtered = VM.CommonData.DataFilters.UserMappedRolesView;

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

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