1
votes

I'm new with the ICollectionView and I'm currently trying to filter a list of object.

Here is my ViewModel :

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

When I add an item in the ObservableCollection "SearchKeywords", the list is correctly updated but the notification "OnPropertyChanged" is not call. How can I do that ?

EDIT : I added the XAML part and the Add methode.

Here is the XAML code that bind the ObservableCollection.

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

And here is the Methode

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

When I add the keyword to "_families.SearchKeywords" the ItemControle get the new item but the filter whish is with the ViewModel do not apply.

2
OnPropertyChanged("SearchKeywords") is not called?... what do you mean? if put a breakpoint it doesn't get hit? throws an error? or what? please be more specific - The One
What is your end goal? It sounds as if you are getting the behavior your desire. Why do you need to fire OnPropertyChanged? - Jason Boyd
@Tuco I updated my post - Thibaud

2 Answers

1
votes

Just subscribe to the CollectionChanged event in your constructor, no need to replace the collection each time.

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

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

Adding an item to an ObservableCollection causes the collection to fire its CollectionChanged event. This unrelated to OnPropertyChanged. Your SearchKeywords property is a property of your ViewModel class - your OnPropertyChanged method is only going to be called if you actually change the value of SearchKeywords, i.e. replace the ObservableCollection with an entirely different ObservableCollection.