archie456
Member
- Joined
- Apr 19, 2020
- Messages
- 11
- Programming Experience
- 1-3
Hi,
I have a small routine has a ListView with textbox - the ListView is bound to a view model. I use a CollectionView to group items in the list box - and the Text box run a filter to hide items depending on whats typed in.
This all works really well - except when the textbox is typed in and the filter is run all of the Expanders that are dynamically created by the CollectionView are closed.
This is caused by having to use ListView.Refresh() to update and display the filtered ListView - Refresh() closes all of the expanders.
Is there any way around this, I can't believe this is the standard way of operation as closing the Expanders each time something is typed is really jarring.
I'd appreciate some advise.
I have a small routine has a ListView with textbox - the ListView is bound to a view model. I use a CollectionView to group items in the list box - and the Text box run a filter to hide items depending on whats typed in.
This all works really well - except when the textbox is typed in and the filter is run all of the Expanders that are dynamically created by the CollectionView are closed.
This is caused by having to use ListView.Refresh() to update and display the filtered ListView - Refresh() closes all of the expanders.
Is there any way around this, I can't believe this is the standard way of operation as closing the Expanders each time something is typed is really jarring.
I'd appreciate some advise.
This is the class of 'Dog' objects that are displayed in the ListView:
public partial class Dogs : ViewModelBase
{
//Group one nesting level
private string _l1grouping;
public string L1grouping
{
get
{
return _l1grouping;
}
set
{
_l1grouping = value;
OnPropertyChanged(nameof(L1grouping));
}
}
//Group two nesting level
private string _l2grouping;
public string L2grouping
{
get
{
return _l2grouping;
}
set
{
_l2grouping = value;
OnPropertyChanged(nameof(L2grouping));
}
}
//Group three nesting level
private string _l3grouping;
public string L3grouping
{
get
{
return _l3grouping;
}
set
{
_l3grouping = value;
OnPropertyChanged(nameof(L3grouping));
}
}
//Name
private string _name;
public string Name
{
get => _name;
set
{
if (_name != value)
{
_name = value;
OnPropertyChanged(nameof(Name));
}
}
}
}
This is the View model, that sets the CollectionViewSource to dynamically create the Expanders and the filter:
class MainWindowViewModel : ViewModelBase
{
public ICollectionView DogsCollectionView { get; }
private ObservableCollection<Dogs> _dogs = new ObservableCollection<Dogs>();
public ObservableCollection<Dogs> Dogs
{
get => _dogs;
set
{
if (value != _dogs)
{
_dogs = value;
OnPropertyChanged(nameof(Dogs));
}
}
}
private string _dogFilter = string.Empty;
public string DogFilter
{
get
{
return _dogFilter;
}
set
{
_dogFilter = value;
OnPropertyChanged(nameof(DogFilter));
DogsCollectionView.Refresh();
}
}
public MainWindowViewModel()
//Constructor
{
DogsCollectionView = CollectionViewSource.GetDefaultView(_dogs);
//Set up filter
DogsCollectionView.Filter = FilterBrowserItems;
//Set up grouping
DogsCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(SaveExpanderState.Dogs.L1grouping)));
DogsCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(SaveExpanderState.Dogs.L2grouping)));
DogsCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(SaveExpanderState.Dogs.L3grouping)));
}
private bool FilterBrowserItems(object obj)
{
if (obj is Dogs check)
{
string nametocheck = check.Name.ToLower();
return nametocheck.Contains(DogFilter.ToLower());
}
return false;
}
}
MainWindow code that creates the Dogs:
public partial class MainWindow : Window
{
MainWindowViewModel DogViewModel
{
get;
set;
}
public MainWindow()
{
InitializeComponent();
DogViewModel = new MainWindowViewModel();
DogViewModel.Dogs.Add(new Dogs() { Name = "Max", L1grouping = "Large", L2grouping = "Brown", L3grouping = "Fast" }); ;
DogViewModel.Dogs.Add(new Dogs() { Name = "Bob", L1grouping = "Small", L2grouping = "Brown", L3grouping = "Slow" });
DogViewModel.Dogs.Add(new Dogs() { Name = "Fido", L1grouping = "Large", L2grouping = "Brown", L3grouping = "Fast" });
DogViewModel.Dogs.Add(new Dogs() { Name = "Brian", L1grouping = "Small", L2grouping = "Brown", L3grouping = "Fast" });
DogViewModel.Dogs.Add(new Dogs() { Name = "Steve", L1grouping = "Large", L2grouping = "Black", L3grouping = "Fast" });
DogViewModel.Dogs.Add(new Dogs() { Name = "Emma", L1grouping = "Large", L2grouping = "Black", L3grouping = "Slow" });
DogViewModel.Dogs.Add(new Dogs() { Name = "Shep", L1grouping = "Large", L2grouping = "Black", L3grouping = "Fast" });
DogViewModel.Dogs.Add(new Dogs() { Name = "Lassy", L1grouping = "Large", L2grouping = "White", L3grouping = "Fast" });
DogViewModel.Dogs.Add(new Dogs() { Name = "Burt", L1grouping = "Large", L2grouping = "White", L3grouping = "Fast" });
DogViewModel.Dogs.Add(new Dogs() { Name = "Siggy", L1grouping = "Small", L2grouping = "White", L3grouping = "Fast" });
DogViewModel.Dogs.Add(new Dogs() { Name = "Heidi", L1grouping = "Small", L2grouping = "Black and White", L3grouping = "Fast" });
DogViewModel.Dogs.Add(new Dogs() { Name = "Loki", L1grouping = "Large", L2grouping = "Yellow", L3grouping = "Fast" });
ListViewDogs.DataContext = DogViewModel;
FilterBoxText.DataContext = DogViewModel;
}
}