Wpf – Binding a nested ListView in WPF

bindinglistviewnestedwpf

I have a simple ListView in which I have another ListView. I can't seem to get the inner listview to bound with my actual List<>, it just shows empty.

My code:

public CollectionViewSource CVSmain {get;set;} //Which is actually a List<MyClassViewModel>, my main ListView bounds to this correctly

public class MyClassViewModel{

    public class MySubClass{
          public string Name{return _name;}
    }

    List<MySubClass> MyList = new List<MySubClass>();
    public string MyText{get;set;}
}

My XAML:

<ListView DataContext="{Binding CVSmain}" ItemsSource="{Binding}" >
        <ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding MyText}" Header="My Text"/>
                <GridViewColumn Header="My List">
                <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <ItemsControl ItemsSource="{Binding MyList}">
                                <ItemsControl.Template>
                                    <ControlTemplate TargetType="ItemsControl">
                                        <ItemsPresenter/>
                                    </ControlTemplate>
                                </ItemsControl.Template>
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel/>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <ListView>
                                            <ListView.View>
                                                <GridView>
                                                    <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name"/>                                                  </GridView>
                                            </ListView.View>
                                        </ListView>
                                        </StackPanel>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

If instead of my second ListView I just put a Textblock with "{Binding Name}", it works. Not sure how to bind this inner ListView, any ideas?

Thanks,

Best Answer

You have three nested controls: the outer ListView, an ItemsControl in the middle, and then the inner ListView. I think the ItemsControl is unnecessary. Try replacing

 <ItemsControl ItemsSource="{Binding MyList}"> 
  ...
 </ItemsControl>

by

 <ListView ItemsSource="{Binding MyList}"> 
     <ListView.View>  
        <GridView>  
            <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name"/> 
        </GridView>  
     </ListView.View> 
 </ListView>

Furthermore, change

List<MySubClass> MyList = new List<MySubClass>(); 

to

public List<MySubClass> MyList { get; set; }

because you can bind to public properties only, not to private fields.

Related Topic