WPF DataGrid通过Parent控件的数据进行单元绑定

时间:2014-10-21 22:34:06

标签: c# wpf binding datagrid

我试图解决这个问题,在黑暗中看不到光。

我在工作区中有工作表(表格)。所有动态(列,表的安装,......)。基于工作表的对象,我想设置数据网格旋转。

VM结构:

ControlVM : 
{ Workspace: [ 
  {  
  Worksheet: [
    {     
      DataViewBindingData: [DataView type],    
      isRotated: rue | false    
    },
    {...} 
  ]
}


<ScrollViewer Grid.Column="0" Grid.Row="0" >
                <ItemsControl ItemsSource="{Binding Path=Workspace}" >
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                        <StackPanel>
                            <Expander Header="{Binding Path=WorksheetName}" ExpandDirection="Down" IsExpanded="True" >
                                    <StackPanel>
                                        <DataGrid CanUserResizeColumns="False" HeadersVisibility="Column" CanUserSortColumns="False" HorizontalAlignment="Stretch" CanUserAddRows="False" Margin="5" 
                                                  SelectionUnit="Cell" SelectionMode="Single" ItemsSource="{Binding Path=DataViewBindingData}" Loaded="DataGrid_Loaded" 
                                                  BeginningEdit="DataGrid_BeginningEdit" />
                                    </StackPanel>
                                </Expander>
                            </StackPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </ScrollViewer>

在我的基本样式中用于控件(标题,单元格)的自定义样式资源中,我试图访问工作表对象的isRotated属性

<Style x:Key="DataGridBase" TargetType="Control">
     <Style.Triggers>
         <!--  TODO cannot find an ancestor for data binding -->
        <DataTrigger Binding="{Binding IsRotated, Converter={StaticResource DebugConverter}, Mode=OneWay}"  Value="True">
            <Setter Property="LayoutTransform">
                <Setter.Value>
                    <TransformGroup>
                        <RotateTransform Angle="-90" />
                        <ScaleTransform ScaleX="-1" ScaleY="1" />
                    </TransformGroup>
                </Setter.Value>
            </Setter>
            <Setter Property="TextOptions.TextFormattingMode" Value="Display" />
        </DataTrigger>
    </Style.Triggers>
</Style >

<!--DataGrid Cells-->
<Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource DataGridBase}" />

这就是问题,我不知道如何访问父控件的数据来绑定属性。谢谢你的建议。

1 个答案:

答案 0 :(得分:0)

DataContext内的隐式DataGridCell实际上是当前DataGridRow的绑定项。因此,在这种情况下,您必须使用RelativeSource来查找DataGrid并将Path设置为DataContext.IsRotatedDataGrid的DataContext是{ {1}}):

Worksheet