【问题标题】:In WPF, how do I bind in the DataGrid.RowDetailsTemplate?在 WPF 中,如何在 DataGrid.RowDetailsTemplate 中进行绑定?
【发布时间】:2011-07-19 15:44:55
【问题描述】:

我有一个绑定到自定义类的 ObservableCollection 的 DataGrid。集合是 ViewModel 中的一个属性,网格可以正常工作,包括对集合的更改。

我想使用 RowDetailsTemplate 在 StackPannel 中显示两个额外的集合,如下所示:

        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <StackPanel Name="GrcidSP" Orientation="Horizontal">
                    <DataGrid Name="ClusterIndexGrid" AutoGenerateColumns="True" 
                              ItemsSource="{Binding ClusterIndexColumns}"
                              CanUserAddRows="False" CanUserResizeRows="False"
                              Width="Auto"/>
                    <DataGrid Name="IndexGrid" AutoGenerateColumns="True" 
                              ItemsSource="{Binding IndexColumns}"
                              CanUserAddRows="False" CanUserResizeRows="False"
                              Width="Auto"/>
                </StackPanel>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>

内部网格的绑定源ClusterIndexColumns 和IndexColumns 也是ViewModel 中的集合。

问题是,当我显示行详细信息时,数据网格是空的,即根本没有加载,没有列或行。

为了了解发生了什么,我用标签替换了内部数据网格:

        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <StackPanel Name="GridSP" Orientation="Horizontal">
                    <Label Content="{Binding}" Width="Auto" Background="AliceBlue"/>
                </StackPanel>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>

并且没有指定绑定源 - {Binding}

标签现在显示自定义类的名称,该类位于作为外部网格源的集合中。

所以不知何故,我需要导航回 ViewModel 作为内部网格的数据上下文。

但是如何,请告诉我?

【问题讨论】:

    标签: datagrid binding rowdetails rowdetailstemplate


    【解决方案1】:

    好的,所以我通过将 ClusterIndexColumns 和 IndexColumns 集合作为属性添加到显示在主网格中的自定义类来解决这个问题。现在,当我单击该行时,会打开 rowdetails 区域并且可以正常加载内部网格。

    顺便说一句,通过将 SelectedIndex 属性绑定到 ViewModel 中的属性,使用

    在主网格中的选定行发生更改时填充集合
    Mode=OneWayToSource
    

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-30
      • 2021-03-24
      • 2012-08-24
      • 1970-01-01
      • 2013-09-29
      • 1970-01-01
      • 2018-03-14
      相关资源
      最近更新 更多