如何在CollectionView过滤中保留SelectedItem?

时间:2017-12-05 09:02:42

标签: c# wpf xaml

所以基本上我有ListView这样:

<ListView x:Name="Contacts" ItemsSource="{Binding Contacts}" 
          SelectionChanged="Contacts_SelectionChanged" SelectionMode="Single"
          SelectedItem="{Binding Contact, Mode=TwoWay}" />

以这种方式应用过滤器:

CollectionViewSource.GetDefaultView(Contacts.ItemsSource).Filter = GeneralUtility.FilterMethod;

现在,当用户选择联系人时,应用程序会使用联系信息填充所有控件。当用户使用过滤器搜索联系人时,SelectedItem将在搜索到的项目时丢失。

Contacts绑定是一个列表:

 public List<Contact> Contacts { get; set; }

SelectedItem是所选的联系人:

private Contact _contact;
//Current contact selected
public Model Contact
{
    get { return _contact; }
    set 
    {
        _contact = value; 
        OnPropertyChanged();
    }
}

一个练习例子:

 > User select the item on the contacts list
 > User search a contact on the list
 > The filter cause the selecteditem to change <-- problem here

如您所见,我需要阻止最后一步自动更改选定项目(只有用户可以更改所选联系人)。

保留用户的SelectedItem的任何想法或解决方案?

0 个答案:

没有答案