There is a listbox and ItemControl (shopControl). When I click on the listbox, I get the name of the clicked element (title). In shopControl is a collection of items.

Elements = new ObservableCollection<ShopItem>(); 

When the name of the element is received - I compare all the elements, leave only those that contain (title), delete the rest.

 var list = (ListBox)sender; TodoItem catTitle = (TodoItem)list.SelectedItem; string title = catTitle.Title.ToLower(); for (int n = Elements.Count - 1; n >= 0; --n) { if (!Elements[n].Slot.Contains(title)) { Elements.RemoveAt(n); } } 

When you re-select an item in the list box - all other items disappear (since the rest were deleted initially) and I get an empty control. How to hide unnecessary elements without deleting, and display only the ones I need?

    1 answer 1

    Use ICollectionView

     private List<MyModel> _myList = new List<MyModel>(); public ICollectionView MyList { get { return _myList; } set { _myList = value; NotifyPropertyChanged("MyList"); } } 

    In the constructor

     MyList.Filter = FilterTask; 

    Create a filtering method

      public bool FilterTask(object value) { var entry = value as MyModel; return entry != null && (string.IsNullOrEmpty(_filterString) || entry.DisplayName.Contains(_filterString)); } 

    Properties for the filter

     private string _filterString = string.Empty; public string FilterString { get { return _filterString; } set { _filterString = value; NotifyPropertyChanged("FilterString"); _myList?.Refresh(); } } 

    Xaml

     <TextBox Text="{Binding FilterString, UpdateSourceTrigger=PropertyChanged}"/> <DataGrid Grid.Row="1" ItemsSource="{Binding Path=MyList, Mode=OneWay}"> <DataGrid.Columns> <DataGridTextColumn Header="Name" Binding="{Binding DisplayName, Mode=OneWay}"/> </DataGrid> 

    Notify on property change

      public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string property) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property)); } 
    • I didn’t understand anything at all (could you do this on my example? .. My code in xaml <ItemsControl x:Name="shopControl" ItemsSource="{Binding Elements... , I already load objects from xml in advance, I parse I add them to the components and through the ObservableCollection. As for the listbox, when I click on it, I sort the items by name in the listbox => to the Elements.Slot words, compare the names, delete them, if not in the category .. Elizabeth
    • I wrote you everything. just wrap your Elements list in ICollectionView and filter it without removing items. You can write like this _myFilteredView = CollectionViewSource.GetDefaultView (Elements); - user2455111
    • So I did not understand ... is missing NotifyPropertyChanged ("FilterString") ;, _myList? .Refresh (); error at the point and requires after Refresh () to put a colon, and a bunch of other errors. In addition, I use a different type of collection for other methods ... - Elizaveta
    • Added NotifyPropertyChanged in response. _myList? .Refresh (); The question mark is the syntax of c # 6 i.e. checking for null can be replaced by if (_myList! = null) _myList.Refresh (); - user2455111