Binding WPF ListView to an Object with Child Collection

bbqchickenrobot

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.

123 456 789 0

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

WPF/MVVM - binding collection of child controls

From Dev

Binding to a collection in WPF

From Dev

wpf Button in listview with binding

From Dev

Model Binding to an object with a collection

From Dev

WPF Binding GridView to Element in collection

From Dev

WPF ListView Binding ItemsSource in XAML

From Dev

Wpf master detail listview binding

From Dev

WPF Binding and Custom ListView and ListViewItems

From Dev

Wpf master detail listview binding

From Dev

WPF ListView binding not finding parameter

From Dev

WPF Listview binding not working with Dictionary

From Dev

ListView - Binding listviewitem data to an object

From Dev

ListView - Binding listviewitem data to an object

From Dev

ContextMenu based on object binding wpf

From Dev

WPF Binding Combobox to LINQ Populated Observable Collection

From Dev

WPF ComboBox ItemTemplate binding to a string collection

From Dev

WPF how to update properly when binding to a collection

From Dev

WPF binding to the same property of multiple objects in a collection

From Dev

WPF DataGrid: Binding a Collection Property to Column

From Dev

Format output on wpf binding to Observable collection

From Dev

WPF - How to binding from two linked collection?

From Dev

WPF binding datagrid to a string observable collection

From Dev

WPF binding to a collection of ViewModels fails to display as expected

From Dev

WPF Combobox Binding broken after Collection repopulation

From Dev

WPF, MVVM, EventBehaviourFactory in ListView, binding event to command

From Dev

WPF: Binding Outside the ListView's ObservableCollection

From Dev

Binding UserControl inside a ListView datatemplate WPF

From Dev

WPF ListView binding with custom type & Insert controls

From Dev

C# WPF Binding Dictionary to Listview

Related Related

  1. 1

    WPF/MVVM - binding collection of child controls

  2. 2

    Binding to a collection in WPF

  3. 3

    wpf Button in listview with binding

  4. 4

    Model Binding to an object with a collection

  5. 5

    WPF Binding GridView to Element in collection

  6. 6

    WPF ListView Binding ItemsSource in XAML

  7. 7

    Wpf master detail listview binding

  8. 8

    WPF Binding and Custom ListView and ListViewItems

  9. 9

    Wpf master detail listview binding

  10. 10

    WPF ListView binding not finding parameter

  11. 11

    WPF Listview binding not working with Dictionary

  12. 12

    ListView - Binding listviewitem data to an object

  13. 13

    ListView - Binding listviewitem data to an object

  14. 14

    ContextMenu based on object binding wpf

  15. 15

    WPF Binding Combobox to LINQ Populated Observable Collection

  16. 16

    WPF ComboBox ItemTemplate binding to a string collection

  17. 17

    WPF how to update properly when binding to a collection

  18. 18

    WPF binding to the same property of multiple objects in a collection

  19. 19

    WPF DataGrid: Binding a Collection Property to Column

  20. 20

    Format output on wpf binding to Observable collection

  21. 21

    WPF - How to binding from two linked collection?

  22. 22

    WPF binding datagrid to a string observable collection

  23. 23

    WPF binding to a collection of ViewModels fails to display as expected

  24. 24

    WPF Combobox Binding broken after Collection repopulation

  25. 25

    WPF, MVVM, EventBehaviourFactory in ListView, binding event to command

  26. 26

    WPF: Binding Outside the ListView's ObservableCollection

  27. 27

    Binding UserControl inside a ListView datatemplate WPF

  28. 28

    WPF ListView binding with custom type & Insert controls

  29. 29

    C# WPF Binding Dictionary to Listview

HotTag

Archive