1

I have some data that has 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 you select an item in the original list. I can't seem to figure out how to get the data binding to work.

Here's what I have so far, (the problem is the {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>
4
  • What output do you get from the binding not working? (And just so you know, "Amount" has one "m"...) Commented Dec 9, 2008 at 22:25
  • It works for me with the same data setup as stackoverflow.com/questions/350214/…. What data set up and sources are you using? Commented Dec 9, 2008 at 23:44
  • It's Compact SQL Server 2008 with a History table and its detail table HistoryItems with a FK relation called FK_History_HistoryItems. It would work with a TreeView and a HierarcicalDataTemplate, but that doesn't seem to work here. Commented Dec 10, 2008 at 0:43
  • Just to confirm, you get the History list showing correctly and it's only the detail which doesn't show? Can you show me how you're declaring your datasource? Commented Dec 10, 2008 at 22:15

3 Answers 3

1

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 datatemplate/view as needed. If you don't want to use ElementName for the binding you could also use RelativeSource but I find ElementName is easier to read and understand.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't think that's quite what I need. I need to bind to the related table on the selected FK from the first table. In fact, the second list view IS INSIDE the SelectedItem of lstHistory, so I don't see how this does much.
0

You need to change your problem line to the following:

<ListView ItemsSource="{Binding FK_History_HistoryItems}">

With that change, the control works beautifully. I have been working on something similar to no avail. I really like your work on this.

Comments

0

In order to get the Trigger to work, You will need to set the ControlTemplate TargetType:

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

Without the TargetType being specified (as a Selectable type), the XAML rendering will be confused...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.