与WPF中嵌套ListViews的分层数据绑定

时间:2021-02-14 14:32:21

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.

我有一些有详细信息表的数据。我希望数据在ListView中呈现。当您在原始列表中选择项目时,我希望详细数据显示为嵌套的ListView。我似乎无法弄清楚如何使数据绑定工作。

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

这是我到目前为止所遇到的(问题是{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>

3 个解决方案

#1


1  

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

如果我正确理解您的问题,您需要绑定到原始列表的SelectedItem:

<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.

然后根据需要设置datatemplate / view。如果您不想使用ElementName进行绑定,您也可以使用RelativeSource,但我发现ElementName更易于阅读和理解。

#2


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.

通过这种改变,控件可以很好地工作。我一直在做类似无济于事的事情。我真的很喜欢你的工作。

#3


0  

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

为了使Trigger工作,您需要设置ControlTemplate TargetType:

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

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

如果没有指定TargetType(作为可选类型),XAML渲染将会混淆...

#1


1  

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

如果我正确理解您的问题,您需要绑定到原始列表的SelectedItem:

<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.

然后根据需要设置datatemplate / view。如果您不想使用ElementName进行绑定,您也可以使用RelativeSource,但我发现ElementName更易于阅读和理解。

#2


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.

通过这种改变,控件可以很好地工作。我一直在做类似无济于事的事情。我真的很喜欢你的工作。

#3


0  

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

为了使Trigger工作,您需要设置ControlTemplate TargetType:

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

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

如果没有指定TargetType(作为可选类型),XAML渲染将会混淆...