Threading issues, settter with ObservableCollection and ListCollectionView

I need assistance in understanding what is happening please. I have an ObservableCollection which I would like to filter according to certain buttons being clicked. (Complete/Busy for example) Now I created a ListCollectionView inside the set to handle the filtering, so the ListCollectionView will be shown on the UI while the ObservableCollection will be used as the data source. The issue with this is the concept that I'd be creating a new ListCollectionView everytime the set is hit (everytime the DisplayList is changed) Here is the snippet of code:
// Constructor
DisplayList = new ObservableCollection<DisplayError>();

// *****
private ObservableCollection<DisplayError> _DisplayList;
private ListCollectionView _DisplayListView;

public ObservableCollection<DisplayError> DisplayList
{
get { return _DisplayList; }
set
{
_DisplayList = value;

_DisplayListView = new ListCollectionView(_DisplayList)
{
Filter = DisplayErrorFilter
};

OnPropertyChanged("DisplayList");
OnPropertyChanged("DisplayListView");
}
}
// Constructor
DisplayList = new ObservableCollection<DisplayError>();

// *****
private ObservableCollection<DisplayError> _DisplayList;
private ListCollectionView _DisplayListView;

public ObservableCollection<DisplayError> DisplayList
{
get { return _DisplayList; }
set
{
_DisplayList = value;

_DisplayListView = new ListCollectionView(_DisplayList)
{
Filter = DisplayErrorFilter
};

OnPropertyChanged("DisplayList");
OnPropertyChanged("DisplayListView");
}
}
Upon trying to initialize my ListCollectionView in the contructor, I ended up with the following code:

// Constructor
DisplayList = new ObservableCollection<DisplayError>();
_DisplayListView = new ListCollectionView(_DisplayList)
{
Filter = DisplayErrorFilter
};


// ******
private ObservableCollection<DisplayError> _DisplayList;
private ListCollectionView _DisplayListView;

public ObservableCollection<DisplayError> DisplayList
{
get { return _DisplayList; }
set
{
_DisplayList = value;
if (_DisplayListView != null)
{
_DisplayListView.Refresh();
}

OnPropertyChanged("DisplayList");
OnPropertyChanged("DisplayListView");
}
}

// Constructor
DisplayList = new ObservableCollection<DisplayError>();
_DisplayListView = new ListCollectionView(_DisplayList)
{
Filter = DisplayErrorFilter
};


// ******
private ObservableCollection<DisplayError> _DisplayList;
private ListCollectionView _DisplayListView;

public ObservableCollection<DisplayError> DisplayList
{
get { return _DisplayList; }
set
{
_DisplayList = value;
if (_DisplayListView != null)
{
_DisplayListView.Refresh();
}

OnPropertyChanged("DisplayList");
OnPropertyChanged("DisplayListView");
}
}
I now get a threading issue upon trying to refresh. I am unsure what is happening.
2 Replies
Thalnos
Thalnos3w ago
how are you calling this constructor, have you tried to run it on the UI thread explicitely? try with Dispatcher.Invoke()
SparkyCracked
SparkyCrackedOP2w ago
It's just my view model:
c#
public ViewModel()
{
// Code here
DisplayList = new ObservableCollection<DisplayError>();
_DisplayListView = new ListCollectionView(_DisplayList)
{
Filter = DisplayErrorFilter
};
// More code
}
c#
public ViewModel()
{
// Code here
DisplayList = new ObservableCollection<DisplayError>();
_DisplayListView = new ListCollectionView(_DisplayList)
{
Filter = DisplayErrorFilter
};
// More code
}
It gets called in my xaml.cs file
c#
public partial class DashboardWindow : Window
{
ViewModel viewModel = new();
// unrelated code
}
c#
public partial class DashboardWindow : Window
{
ViewModel viewModel = new();
// unrelated code
}

Did you find this page helpful?