I'm having trouble filtering hierarchical data that's being displayed in nested xaml templates.
I've got a ObservableCollection<Foo> Foos
, that I'm displaying in XAML.
Lets say Foo looks like:
class Foo
{
public ObservableCollection<Bar> Bars;
}
class Bar
{
public ObservableCollection<Qux> Quxes;
}
I'm displaying Foos with the following xaml:
<Grid>
<Grid.Resources>
<CollectionViewSource x:Key="MyCVS" Source="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.UnifiedSymbols}" Filter="MyCVS_Filter" />
<DataTemplate x:Key="NestedTabHeaderTemplate">
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
<DataTemplate x:Key="NestedTabContentTemplate">
<ListBox ItemsSource="{Binding Path=Quxes}" DisplayMemberPath="Name"/>
</DataTemplate>
<DataTemplate x:Key="TopLevelTabHeaderTemplate">
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
<DataTemplate x:Key="TopLevelTabContentTemplate">
<TabControl ItemsSource="{Binding Path=Bars}"
ItemTemplate="{StaticResource NestedTabHeaderTemplate}"
ContentTemplate="{StaticResource NestedTabContentTemplate}"
/>
</DataTemplate>
</Grid.Resources>
<TabControl ItemSource="{Binding correct binding for my control's collection of Foos}"
ItemTemplate="{StaticResource TopLevelTabHeaderTemplate}"
ContentTemplate="{StaticResource TopLevelTabContentTemplate}"
x:Name="tabControl"
/>
</Grid>
To put it into words, there's a tab control, with a tab for each Foo. Each Foo is a tab control, with each Bar it contains in it's own tab. Each Bar contain's a listbox of its Quxes.
or:
______ ______ ______
| Foo1 | Foo2 | Foo3 |
|______ ______ |
| Bar1 | Bar2 |______|
| | qux1 ||
| | qux2 ||
| | qux3 ||
----------------------
I also have a TextBox that I'd like to use to filter this breakdown. When I type in the text box, I'd like to filter the quxes so those not containing the text wouldn't be visible. Ideally Bar
tabs would also be hidden if they have no visible quxes, and Foo
tabs hidden when they have no visible Bar
s
I have considered two approaches:
On my text box's TextChanged event, I loop through my Foo's asking for the corresponding (static) TabControl's CollectionViewSource:
foreach(Foo foo in tabControl.Items)
{
var tabItem = tabControl.ItemContainerGenerator.ContainerFromItem(foo); // This is always of type TabItem
// How do I get the TabControl that will belong to each of Foo's Bar's?
}
I tried setting the Filter via xaml, by changing this line:
<ListBox ItemsSource="{Binding Path=Quxes}" DisplayMemberPath="Name">
to this,
<CollectionViewSource x:Key="MyCVS" Source="?????" Filter="MyCVS_Filter" />
...
<ListBox ItemsSource="{Binding Source={StaticResource MyCVS}}" DisplayMemberPath="Name">
I've tried a number of things where I have "?????" but I cannot correctly bind to the ListBox's datacontext and appropriate Quxes member. Nothing I try results in the quxes being displayed, and I get no errors on the console. Even If I could get this approach to work, I'm not sure how I would re-trigger this filter when the text in the search box changed.
Any advice or direction would be appreciated.
Edit
Finally I've got it working with your requirements.
Here is the link to the updated project.
(edit by luke)
This is the (excellent) solution I ended up going with, so I'm going to extract the important parts, and actually make them part of the post here:
The key xaml portion ends up looking like this:
<CollectionViewSource x:Key="FooCVS" x:Name="_fooCVS" Source="{Binding Foos, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type WpfApplication1:MainWindow}}}" Filter="_fooCVS_Filter"/>
<CollectionViewSource x:Key="BarCVS" x:Name="_barCVS" Source="{Binding Bars, Source={StaticResource FooCVS}}" Filter="_barCVS_Filter"/>
<CollectionViewSource x:Key="QuxCVS" x:Name="_quxCVS" Source="{Binding Quxs, Source={StaticResource BarCVS}}" Filter="_quxCVS_Filter"/>
I set the respective control to each one of these views as the control's ItemSource
. The magic is in the binding of each CVS. Each CVS gets the data context for the control/templated control in which appears, so you can use the the real name of the bound object's collection. I'm not sure I understand why binding the source of that source binding to itself (the CVS) works, but it does so beautifully.
The code for the filter TextBox
then becomes something like:
private void filterTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
var cvs = TryFindResource("FooCVS") as CollectionViewSource;
if (cvs != null)
{
if (cvs.View != null)
cvs.View.Refresh();
}
cvs = TryFindResource("QuxCVS") as CollectionViewSource;
if (cvs != null)
{
if (cvs.View != null)
cvs.View.Refresh();
}
cvs = TryFindResource("BarCVS") as CollectionViewSource;
if (cvs != null)
{
if (cvs.View != null)
cvs.View.Refresh();
}
}
Excellent solution as it requires no changing of the underlying objects or hierarchy.
I think you should expose an ICollectionView
from your View-Model instead of (or in addition to) an ObservableCollection
. This would bring all the business logic involved with filtering/sorting into the VM, which is the right place for it.
You can get the ICollectionView
of a collection by creating a CollectionViewSource
, setting its Source
property to the collection and retrieving the View
property.
(Update) Here's some sample code:
class Foo
{
public Foo()
{
_bars = new ObservableCollection<Bar>();
Bars = new CollectionViewSource { Source = _bars }.View;
}
private ObservableCollection<Bar> _bars;
public ICollectionView Bars { get; private set; }
public void Filter(string quxName)
{
Bars.Filter = o => ((Bar)o).Quxes.Any(q => q.Name == quxName);
foreach (Bar bar in Bars)
{
bar.Filter(quxName);
}
}
}
class Bar
{
private ObservableCollection<Qux> _quxes;
public ICollectionView Quxes { get; private set; }
public void Filter(string quxName)
{
Quexs.Filter = o => ((Qux)o).Name == quxName;
}
}
class Qux
{
public string Name { get; set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With