2016-11-30 198 views
5

我有一个WPFDataGrid代表XAML。我为我的网格TableView使用RowStyle,但也需要为特定单元格设置一些属性。我需要这些单元格具有行样式的属性,并将单元格样式中的额外属性应用于这些属性之上。基于WPF中的RowStyle的CellStyle

我需要的是这样的事情,虽然这并不工作,因为它在说:

目标类型“CellContentPresenter”是无法转换为基类型“GridRowContent”

<Style x:Key="MyGridRowStyle" 
    BasedOn="{StaticResource {themes:GridRowThemeKey ResourceKey=RowStyle}}" 
    TargetType="{x:Type dxg:GridRowContent}"> 
    <Setter Property="Height" 
     Value="25" /> 
     <Style.Triggers> 
     ... 
    </Style.Triggers> 
</Style> 

<Style x:Key="MyCellStyle" 
    BasedOn="{StaticResource MyGridRowStyle}" 
    TargetType="{x:Type dxg:CellContentPresenter}"> 
    <Style.Triggers> 
     ... 
    </Style.Triggers> 
</Style> 

我也试过没有指定BasedOn属性为MyCellStyle,但这也行不通。

我使用MyCellStyle这样的:

<dxg:GridColumn Header="My Header" 
       FieldName="MyFieldName" 
       Width="100" 
       CellStyle="{StaticResource MyCellStyle}" /> 

MyGridRowStyle这样的TableView

RowStyle="{StaticResource MyGridRowStyle}" 

我怎样才能使单元格样式只更改指定的属性在MyCellStyle和使用其他属性在MyGridRowStyle中指定的值?

回答

2

基于WPF正常DataGrid中,你可以试试这个,扩大它dxgDataGridCellContentControl衍生(即从Content孩子)。 类别DataGridRow源自Control

现在你可以尝试以下方法:

<Style x:Key="BaseStyle" TargetType="Control" > 
    <!-- Maybe add BaseStyle/theme here with BasedOn --> 
    <Setter Property="Height" Value="25" /> 
    <!-- Row and Column defaults --> 
</Style> 
<Style x:Key="MyGridRowStyle" BasedOn="{StaticResource BaseStyle}" 
     TargetType="DataGridRow"> 
    <!-- Row specific implementation --> 
</Style> 
<Style x:Key="MyCellStyle" BasedOn="{StaticResource BaseStyle}" 
     TargetType="DataGridCell"> 
    <!-- Column specific implementation --> 
</Style> 

摘要:都和Column类 使用基本类型的BaseStyle和使用这一个为BasedOn。对于dxg你可以自己扩展它...

+1

这是很容易的最简单的解决方案和我正在寻找的东西。这很好,谢谢! –

4

您不能将CellContentPresenter样式基于GridRowContent样式。这是两种完全不同的类型,仅仅因为它们可能碰巧具有一些具有相同名称的属性,但它们仍然是完全不同的和独立的属性,彼此之间没有关系。

你能做的最好的事情是确定共同作为单独的资源,在这两种风格使用这些资源,例如:

<Window.Resources> 
    <s:Double x:Key="commonHeight">25</s:Double> 
    <SolidColorBrush x:Key="commonBg">Red</SolidColorBrush> 

    <Style x:Key="MyCellStyle" TargetType="{x:Type dxg:CellContentPresenter}"> 
    <Setter Property="Height" Value="{StaticResource commonHeight}" /> 
    <Setter Property="Background" Value="{StaticResource commonBg}" /> 
    </Style> 

    <Style x:Key="MyGridRowStyle" BasedOn="{StaticResource {themes:GridRowThemeKey ResourceKey=RowStyle}}" TargetType="{x:Type dxg:GridRowContent}"> 
    <Setter Property="Height" Value="{StaticResource commonHeight}" /> 
    <Setter Property="Background" Value="{StaticResource commonBg}" /> 
</Style> 
</Window.Resources> 

但你仍然需要定义所有制定者两个样式。

2

我对这个问题的理解:单元格样式的值应该根据它所在行的样式中的值而改变。

触发器关的价值

下面是一个简单的工作示例(通过改变DataGridRow背景红色测试它,你会发现,电池的前景变为蓝色):

<DataGrid> 
    <DataGrid.RowStyle> 
     <Style TargetType="DataGridRow"> 
      <Setter Property="Background" Value="White"/> 
     </Style> 
    </DataGrid.RowStyle> 
    <DataGrid.CellStyle> 
     <Style TargetType="DataGridCell"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow, Mode=FindAncestor}, Path=Background}" Value="Red"> 
        <Setter Property="Foreground" Value="Blue"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </DataGrid.CellStyle> 
</DataGrid> 

你可以做类似的绑定,直接将CellStyle的属性设置为所在行的属性值。

相对直接绑定属性

<DataGrid x:Name="dataGrid1" ItemsSource="{Binding Collection}"> 
    <DataGrid.RowStyle> 
     <Style TargetType="DataGridRow"> 
      <Setter Property="Height" Value="20"/> 
     </Style> 
    </DataGrid.RowStyle> 
    <DataGrid.CellStyle> 
     <Style TargetType="DataGridCell"> 
      <Setter Property="FontSize" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow, Mode=FindAncestor}, Path=Height}"/> 
     </Style> 
    </DataGrid.CellStyle> 
</DataGrid> 

说明

RelativeBinding工作由于事实DataGridCells是DataGridRows的最终孩子们可在该截图DataGrid中的可视化树被视为:

DataGridCells are eventual children of DataGridRows