I am building a WPF application that takes in rows of data, and outputs them into different tabs in the GUI based on the data contained in the row. However, the tabs aren’t known until runtime, so I need to dynamically build an unknown number of tabs with collection views with different filters off of my main ObservableCollection.
The problem I have been running in to is that using ListCollectionViews I need a predicate filter but I do not know of a way to have a dynamic predicate based on a local variable? I tried variable capturing, but that just changes all of my filters each time a new tab is added.
//class variables
string currTab;
public ObservableCollection<MyData> myCollection = new ObservableCollection<myData>();
private void DataAdd(object sender, RoutedEventArgs e)
{
currTab = inputData.ToString();
ListCollectionView c = new ListCollectionView(myCollection);
c.Filter = new Predicate<object>(MyFilter);
}
public bool MyFilter(object foo)
{
if (foo).ToString() != currTab)
return false;
else
return true;
}
I also tried using lambda expressions and with ICollectionView, but the collection doesn’t update with new values, so I just see empty tabs.
CollectionView c = new CollectionViewSource { Source = myCollection.Where(z => z.ToString() == tabName) }.View;
Is there a way to make either of these approaches work? Or a better way to do this?
it turns out I just needed to use a local variable for the predicate