I have the following object definitions:
public class FilterItem
{
public string Type{get;set;}
public List<string> Items{get;set;}
}
public class FiltersDataContext
{
public string SearchText{get;set;}
public List<FilterItem> Filters{get;set;}
}
I need to bind a ListView to the FiltersDataContext.Filters.Items
child collection.
So far my xaml looks like this:
<ListView Name="ResearchFilters" ItemsSource="{Binding FiltersDataContext.Filters}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="FilterPanel">
<CheckBox Content="{Binding Items, Mode=OneWay}" Tag="{Binding Path=Type, Mode=OneWay}" Checked="FilterCheckBox_OnCheck" Unchecked="FilterCheckBox_UnChecked" Click="FilteringResultSet"></CheckBox>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
However, the ListView only ever displays a single CheckBox. Wondering where I'm going wrong.
You can achieve this without using ListView
, but instead using ItemsControl
<ItemsControl ItemsSource="{Binding Path=Items}" ItemTemplate="{StaticResource CheckBoxItemsControlTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
And in your resource dictionary, add this
<DataTemplate x:Key="CheckBoxTemplate">
<CheckBox Content="{Binding Items, Mode=OneWay}" Tag="{Binding Path=Type, Mode=OneWay}"/>
</DataTemplate>
<DataTemplate x:Key="CheckBoxItemsControlTemplate">
<ItemsControl ItemsSource="{Binding Path=FiltersDataContext.Filters}" ItemTemplate="{StaticResource CheckBoxTemplate}">
</ItemsControl>
</DataTemplate>
</ResourceDictionary>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments