保留的SelectedItem CollectionViewSource的过滤过程 [英] Keep SelectedItem during filter of CollectionViewSource

查看:216
本文介绍了保留的SelectedItem CollectionViewSource的过滤过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框正被这样的过滤器:

I have a listbox that is being filter like this:

var view = CollectionViewSource.GetDefaultView(FilterSource);
view.Filter = FilterCode;

我遇到了一个问题,即的SelectedItem 当过滤器使用视图模型code这样的迷失:

I am running into a problem where the SelectedItem is getting lost when the filter is used with viewmodel code like this:

VM
{
    public ObservableCollection<Model> Items{get;set;}
    public Model SelectedItem
    {
        get{return _selectedItem;}
        set{_selectedItem = value; NotifyPropertyChanged();}
    }
}

当过滤器被应用,的SelectedItem 设置为。但是,我想保持选定项目,除非用户实际点击关闭它。如果过滤器被移除,然后所选择的项目应仍然被选中。该模式确实有一个 IsSelected 属性,我一直在努力想办法,以查询 IsSelected 属性。但是,那么该视图的结合是行不通的我想到....或者至少我会在圈子思维方式,它不能

When the filter is applied, the SelectedItem is set to null. However, I want to keep that selected item unless the user actually clicks off of it. If the filter is removed, then the selected item should still be selected. The Model does have an IsSelected property, and I have been trying to think of ways to query for the IsSelected property. However, then the view's binding would not work the way I expect....or at least I am going in circles thinking it cannot

推荐答案

我只能在这里完成的修复方法是在的SelectionChanged 事件如下:

My only way of accomplishing a fix here is the following in the SelectionChanged event:

if (e.AddedItems.Count == 0 && e.RemovedItems.Count >= 0)
    SpecialtyListBox.SelectedItem = e.RemovedItems[0];

不过,这似乎真的哈克和力量,必须永远存在一个初始选择了一个选择一次的项目。在这种情况下,可能的工作,但我还是想看看是否有人有一个更好的解决方案?

But, this seems really hacky and forces that there must always be an item selected once an initial one is selected. In this case, that might work, but I would still like to see if anybody has a better solution?

这篇关于保留的SelectedItem CollectionViewSource的过滤过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆