【问题标题】:Hierarchical data binding with nested ListViews in WPFWPF 中嵌套 ListView 的分层数据绑定
【发布时间】:2010-09-26 02:29:26
【问题描述】:

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

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

【问题讨论】:

  • 绑定不工作得到什么输出? (你知道,“Amount”有一个“m”...)
  • 它适用于与stackoverflow.com/questions/350214/… 相同的数据设置。您使用什么数据设置和来源?
  • 它是 Compact SQL Server 2008,带有一个 History 表,其详细表 HistoryItems 具有称为 FK_History_HistoryItems 的 FK 关系。它可以与 TreeView 和 HierarcicalDataTemplate 一起使用,但在这里似乎不起作用。
  • 只是为了确认一下,你得到了正确显示的历史列表,它只是没有显示的细节?你能告诉我你是如何声明你的数据源的吗?

标签: wpf data-binding .net-3.5 listview


【解决方案1】:

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

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

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

【讨论】:

  • 我认为这不是我所需要的。我需要从第一个表中绑定到所选 FK 上的相关表。事实上,第二个列表视图在 lstHistory 的 SelectedItem 内,所以我看不出这有多大作用。
【解决方案2】:

您需要将问题行更改为以下内容:

<ListView ItemsSource="{Binding FK_History_HistoryItems}">

有了这种变化,控件可以很好地工作。我一直在做类似的事情,但无济于事。我真的很喜欢你在这方面的工作。

【讨论】:

    【解决方案3】:

    为了让触发器工作,您需要设置 ControlTemplate TargetType:

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

    如果没有指定 TargetType(作为 Selectable 类型),XAML 呈现会混淆...

    【讨论】:

      猜你喜欢
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多