Search code examples
wpfeventsdatagriddatatemplate

populating a listview within a datatemplate


I am currently having problems accessing the listview within a DataTemplate, each row has its unique list, for example Sam has certain documents that needs click events, I just cant access the name of the control.

Through research I have discovered command and using the visual tree helper, however cant access the list to bind data with the itemsource.

 <DataGrid Name="dgUsers">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Client" Binding="{Binding name1}"/>
            <DataGridTextColumn Header="Land Line" Binding="{Binding landline}"/>         
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate x:Name="datatemplate">
                <DockPanel Background="GhostWhite">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="200"/>
                            <ColumnDefinition Width="200"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>

                        <ListView x:Name="lvPipeline" Margin="10,82,10,156" Grid.Column="2" >
                            <ListView.View>
                                <GridView>

Solution

  • In xaml you can access named controls in a template like yours only in the template and not from the outside. Not sure if i understand the general application context all the way but I would use MVVM and create a model and viewmodel for objects like Sam and put Sam's documentlist in the viewmodel and bind it to the listbox in the datatemplate.

    My approach would be something like this:

    public class UserViewModel
    {
        public string Name {get;set}
        public ObservableCollection<string> Documents {get;set;}
        public string SelectedDocument {get;set}
    
    }
    
    
    
      <DataGrid Name="dgUsers" ItemsSource="{Binding Users}">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Client" Binding="{Binding Name}"/>
                  <!--  <DataGridTextColumn Header="Land Line" Binding="{Binding LandLine}"/>    -->     
                </DataGrid.Columns>
                <DataGrid.RowDetailsTemplate>
                    <DataTemplate x:Name="datatemplate">
                        <DockPanel Background="GhostWhite">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="200"/>
                                    <ColumnDefinition Width="200"/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
    
                                <ListView ItemsSource="{Binding Documents}" SelectedItem="{Binding SelectedDocument}" Margin="10,82,10,156" Grid.Column="2" >
                                     <ListView.View>
                                        <GridView>
    

    After that you can use the SelectedDocument property and bind it to a e.g. document viewer.