【问题标题】:How to bind to ICollection of the parent binding in DataGrid RowDetails? (MVVM)如何绑定到 DataGrid RowDetails 中父绑定的 ICollection? (MVVM)
【发布时间】:2014-07-10 18:04:02
【问题描述】:

我有一个数据网格,其中包含行详细信息以显示第二行以及该行的历史信息。我在 XAML 中有这段代码:

<DataGrid HorizontalAlignment="Stretch" Margin="5,5,5,5" Grid.Row="1" VerticalAlignment="Stretch"
                  ItemsSource="{Binding DgdTareas}"
                  SelectedIndex="{Binding DgdTareasSelectedIndex}"
                  RowDetailsVisibilityMode="{Binding DgdTareasDetallesVisibility}">


    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <DataGrid AutoGenerateColumns="false" HorizontalAlignment="Stretch" Margin="5,5,5,5" Name="dgdTareasPiezasHistorico" VerticalAlignment="Stretch" Grid.Row="1"
                                  ItemsSource="{Binding Path=TareasPiezasHistorico}">
               <DataGrid.Columns>
                   <DataGridTextColumn Header="IDEstado" Binding="{Binding IDEstado, Mode=TwoWay}" />
               </DataGrid.Columns>
           </DataGrid>
       </DataTemplate>
   </DataGrid.RowDetailsTemplate>
</DataGrid>

主dataGrid,绑定一个Tareas类型的集合。此对象具有 TareasHistorico 类型的 ICollection。

所以我希望第二个dataGrid的itemsSource是Tareas.TareasHistorico,但是我无法绑定这个属性。

谢谢。

【问题讨论】:

  • 也许只是拼写错误?在 XAML sn-p 中,您正在绑定 TareasPiezasHistorico,但在您声明要绑定 Tareas.TareasHistorico 的问题中。

标签: wpf mvvm datagrid


【解决方案1】:

用作DataGrid.RowDetailsTemplateDataTemplate 与其DataContext 具有与DataGrid.ItemsTemplate 相同的对象集,即来自您的数据绑定集合中的Tareas 类型的项目。因此,您需要做的就是在内部DataGrid.ItemsSource 属性的Binding Path 中使用Tareas 类的ICollection 属性的名称:

<DataGrid AutoGenerateColumns="false" HorizontalAlignment="Stretch" Margin="5,5,5,5" 
   Name="dgdTareasPiezasHistorico" VerticalAlignment="Stretch" Grid.Row="1"
   ItemsSource="{Binding NameOfICollectionPropertyInTareasClass}">
   ...
</DataGrid>

当然,您还必须确保该属性已声明为public

【讨论】:

    猜你喜欢
    • 2016-11-05
    • 2013-02-28
    • 2015-08-06
    • 2018-02-07
    • 1970-01-01
    • 2021-07-07
    • 2021-08-31
    • 2013-03-24
    • 2023-04-02
    相关资源
    最近更新 更多