【问题标题】:Update a ViewModel and pass filter when adding item to ObservableCollection<string>将项目添加到 ObservableCollection<string> 时更新 ViewModel 并通过过滤器
【发布时间】:2017-08-09 15:57:48
【问题描述】:

我是 ICollectionView 的新手,目前正在尝试过滤对象列表。

这是我的视图模型:

public class ViewModel : INotifyPropertyChanged
{
    private ObservableCollection<RevitFamily> _myData;
    public ObservableCollection<RevitFamily> MyData
    {
        get { return _myData; }
    }

    string searchName = string.Empty;
    ObservableCollection<string> searchKeywords = new ObservableCollection<string>();
            public string SearchName
    {
        get { return searchName; }
        set
        {
            searchName = value;
            myDataView.Filter = FilterName;
            OnPropertyChanged("SearchName");
        }
    }
    public ObservableCollection<string> SearchKeywords
    {
        get { return searchKeywords; }
        set
        {
            searchKeywords = value;
            myDataView.Filter = FilterName;
            OnPropertyChanged("SearchKeywords");
        }
    }
    ICollectionView myDataView;
    public ViewModel()
    {
        _myData = new ObservableCollection<RevitFamily>();
        myDataView = CollectionViewSource.GetDefaultView(_myData);
        //when the current selected changes store it in the CurrentSelectedPerson
        myDataView.CurrentChanged += delegate
        {
            //stores the current selected person
            CurrentSelectedFamily = (RevitFamily)myDataView.CurrentItem;
        };
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

当我在 ObservableCollection“SearchKeywords”中添加一个项目时,列表已正确更新,但未调用通知“OnPropertyChanged”。我该怎么做?

编辑:我添加了 XAML 部分和 Add 方法。

这是绑定 ObservableCollection 的 XAML 代码。

<Border Grid.Row="6" Grid.ColumnSpan="3" Height="100">
   <ItemsControl x:Name="ListKeywords" ItemsSource="{Binding SearchKeywords, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
      <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
            <WrapPanel />
         </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
      <ItemsControl.ItemTemplate>
         <DataTemplate>
            <local:CrossLabel MyLabel="{Binding}" Remove="Kw_Remove"/>
         </DataTemplate>
      </ItemsControl.ItemTemplate>
   </ItemsControl>
</Border>

这是方法

private void Kw_Add(object sender, RoutedEventArgs e)
{
    if (!_families.SearchKeywords.Contains(this.Keywords.Text))
    {
        _families.SearchKeywords.Add(this.Keywords.Text);
    }
}

当我将关键字添加到“_families.SearchKeywords”时,ItemControle 会获取新项目,但使用 ViewModel 的过滤器不适用。

【问题讨论】:

  • OnPropertyChanged("SearchKeywords") 没有被调用?...你是什么意思?如果放一个断点它不会被击中?抛出错误?要不然是啥?请更具体
  • 你的最终目标是什么?这听起来就好像你得到了你想要的行为。为什么要开除OnPropertyChanged
  • @Tuco 我更新了我的帖子

标签: c# wpf


【解决方案1】:

只需在你的构造函数中订阅CollectionChanged事件,无需每次都替换集合。

public ViewModel()
{
    searchKeywords.CollectionChanged += searchKeywords_CollectionChanged;
}

void searchKeywords_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    throw new NotImplementedException();
}

【讨论】:

  • 谢谢,我需要调用 MyDataView.Refresh() 而不是 throw new 来应用过滤器?编辑:把 MyDataView.Filter 和一切都好!谢谢!
  • @Thibaud 你在事件发生时做你需要做的任何事情......抛出异常可能不是你的实际需要:)
  • 是的 ^^ 我已经把 MyDataView.Filter 和一切都很好!谢谢你 !标记为已解决:)
【解决方案2】:

将项目添加到ObservableCollection 会导致集合触发其CollectionChanged 事件。这与OnPropertyChanged 无关。您的 SearchKeywords 属性是您的 ViewModel 类的属性 - 只有在您实际更改 SearchKeywords 的值时才会调用您的 OnPropertyChanged 方法,即将 ObservableCollection 替换为完全不同的 ObservableCollection .

【讨论】:

  • 我不想替换所有集合,而是添加/删除其中的项目。我更新了我的帖子
猜你喜欢
  • 1970-01-01
  • 2019-07-12
  • 1970-01-01
  • 1970-01-01
  • 2014-03-21
  • 1970-01-01
  • 1970-01-01
  • 2015-07-13
  • 1970-01-01
相关资源
最近更新 更多