简体   繁体   English

WPF DataGrid单元格通过父控件中的数据进行绑定

[英]WPF DataGrid Cell binding by data from Parent control

I am trying to sort this out and cannot see light in the dark. 我正在尝试解决此问题,看不到黑暗中的光线。

I have worksheets (tables) in workspace. 我在工作区中有工作表(表)。 All dynamic (columns, mount of tables, ...). 所有动态的(列,表的安装,...)。 Based on object for worksheet I would like to set datagrid rotation. 基于工作表的对象,我想设置数据网格旋转。

VM structure: 虚拟机结构:

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>

And inside custom styles resources in my base style for control (header, cells), I am trying to access isRotated property of worksheet object 在我的基本样式控件(标题,单元格)中的自定义样式资源中,我试图访问工作表对象的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}" />

And thats the problem, I dont know how to access parent control's data to bind the property. 那就是问题,我不知道如何访问父控件的数据来绑定属性。 Thanks for suggestions. 感谢您的建议。

The implicit DataContext inside DataGridCell is actually the current DataGridRow's bound item. 隐式DataContextDataGridCell实际上是当前DataGridRow的绑定项。 So in this case you have to use RelativeSource to find the DataGrid and set Path to DataContext.IsRotated (the DataGrid 's DataContext is a Worksheet ): 因此,在这种情况下,您必须使用RelativeSource查找DataGrid并将Path设置为DataContext.IsRotatedDataGrid的DataContext是一个Worksheet ):

<DataTrigger Binding="{Binding DataContext.IsRotated, 
                       RelativeSource={RelativeSource AncestorType=DataGrid}
                       Converter={StaticResource DebugConverter}, Mode=OneWay}"
              Value="True">
<!-- ... -->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM