【问题标题】:Child dataGrid dataContext is being set to parent dataGrid causing binding Error子 dataGrid dataContext 被设置为父 dataGrid 导致绑定错误
【发布时间】:2014-10-15 19:12:50
【问题描述】:

我有一个父级和子级 DataGrid:

 <DataGrid Name="DispositionNCRsGrid"
                              ItemContainerStyle="{StaticResource MainDataGridItemStyle}"
                              ItemsSource="{Binding Path=ParentSource}"
                               >
<DataGrid.RowDetailsTemplate>
                 <DataTemplate>
                       <DataGrid SelectionMode="Single"
                                ItemsSource="{Binding MyItemSource}"
                                   Style="{DynamicResource MainDataGridStyle}" 
                                         HorizontalAlignment="Center" >

现在的问题是子网格在 ParentSource 中查找 MyItemSource 而不是当前视图模型。

如何重置子数据网格以访问当前 ViewModel 中的数据

【问题讨论】:

  • 所以您希望详细信息 DataGrid 从父 DataGrid 获取 ParentSource 的同一来源获取 MyItemSource
  • @dkozl,是的,我希望孩子从同一个 VM 获取 MyItemSource

标签: c# wpf datagrid


【解决方案1】:

如果您想从主DataGrid 采用ParentSource 的同一视图模型中获取MyItemSource 的详细信息DataGrid,那么您可以使用RelativeSource 绑定并向上可视化树查找父DataGrid并使用它的DataContext

ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.MyItemSource}"

【讨论】:

    猜你喜欢
    • 2015-01-21
    • 2012-05-26
    • 2012-12-25
    • 2012-11-22
    • 2013-11-16
    • 1970-01-01
    • 2013-07-24
    • 2016-09-22
    • 2012-02-12
    相关资源
    最近更新 更多