Binding hierarchical data with nested ListViews in WPF

I have data containing a detail table. I want the data to be presented in a ListView. I want the detail data to appear as a nested ListView when an item is selected in the original list. I can't figure out how to get the data binding to work.

Here's what I have so far (the problem is {Binding Path=FK_History_HistoryItems}

):

<ListView Name="lstHistory" ItemsSource="{Binding Source={StaticResource History}}" SelectionChanged="lstHistory_SelectionChanged">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="Name" Width="100" />
            <GridViewColumn DisplayMemberBinding="{Binding Path=Description}" Header="Description" Width="150" />
            <GridViewColumn DisplayMemberBinding="{Binding Path=Total, Converter={StaticResource moneyConvert}}" Header="Total" Width="100" />
            <GridViewColumn DisplayMemberBinding="{Binding Converter={StaticResource categoryAggregate}}" Header="Categories" Width="100" />
        </GridView>
    </ListView.View>
    <ListView.Resources>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <Border>
                            <StackPanel>
                                <Border Name="presenter"
                                        Background="{TemplateBinding Background}"
                                        BorderBrush="{TemplateBinding BorderBrush}"
                                        BorderThickness="{TemplateBinding BorderThickness}"
                                        Padding="{TemplateBinding Padding}">
                                    <GridViewRowPresenter />
                                </Border>
                                <Border Name="details" Visibility="Collapsed" Margin="5"
                                        BorderBrush="Black" BorderThickness="2">
                                    <StackPanel Margin="5">
                                        <ListView ItemsSource="{Binding Path=FK_History_HistoryItems}">
                                            <ListView.View>
                                                <GridView>
                                                    <GridViewColumn DisplayMemberBinding="{Binding Path=Ammount}" Header="Ammount" Width="100" />
                                                    <GridViewColumn DisplayMemberBinding="{Binding Path=Category}" Header="Category" Width="100" />
                                                </GridView>
                                            </ListView.View>
                                        </ListView>
                                    </StackPanel>
                                </Border>
                            </StackPanel>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter TargetName="details" Property="Visibility" Value="Visible" />
                                <Setter TargetName="presenter" Property="Background" Value="Navy"/>
                                <Setter TargetName="presenter" Property="TextElement.Foreground" Value="White" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.Resources>
</ListView>

      

+1


source to share


3 answers


If I understand your question correctly, you need to bind to the SelectedItem of the original list:

<ListView ItemsSource="{Binding ElementName=lstHistory, Path=SelectedItem}">

      



And then set the data type / view you want. If you don't want to use ElementName for binding, you can also use RelativeSource, but I find ElementName easier to read and understand.

+1


source


You need to change your problem line like this:

<ListView ItemsSource="{Binding FK_History_HistoryItems}">

      



With this change, the controls work nicely. I've been working on something similar to no avail. I really love your work on this.

0


source


To get the Trigger to work, you will need to set the ControlTemplate TargetType:

<ControlTemplate TargetType="{x:Type ListViewItem}">

      

Without a TargetType (as a Selectable type) XAML rendering will get confused ...

0


source







All Articles