【问题标题】:How to Apply a Cell Style to DataGrid Cell如何将单元格样式应用于 DataGrid 单元格
【发布时间】:2013-09-08 12:51:01
【问题描述】:

我有以下DataGrid

<DataGrid x:Name="cultureDataGrid" 
          Grid.Row="1" 
          CellStyle="{StaticResource DataGridCell}"
          ItemsSource="{Binding Cultures, 
                                NotifyOnSourceUpdated=True, 
                                UpdateSourceTrigger=PropertyChanged, 
                                Mode=TwoWay, 
                                IsAsync=True}" 
          Style="{x:Null}" >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Code" Binding="{Binding Code}" IsReadOnly="True"/>
        <DataGridTextColumn Header="Language" Binding="{Binding Language}" IsReadOnly="True"/>
        <DataGridTextColumn Header="LocalName" Binding="{Binding LocalName}" IsReadOnly="True"/>
    </DataGrid.Columns>
</DataGrid>

我有以下单元格样式来改变选中的Backcolor

<Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
    <Setter Property="Background" Value="White"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
    </Style.Triggers>
</Style>

我已尝试如上所示应用CellStyle="{StaticResource DataGridCell}",并使用DynamicResource,但资源无法解析。我已导入正确的资源字典,因为其他样式正在运行我在这做错了什么?

感谢您的宝贵时间。

【问题讨论】:

  • 假设您的 Style 保存在资源字典中,您不需要添加 x:Key 属性来解决它吗?

标签: c# wpf datagrid styles


【解决方案1】:

由于您的Style 没有Key,因此您不必在DataGrid 上设置CellStyle,它将默认应用于所有DataGridCell

如果您不希望它默认应用于所有DataGridCell,则将样式设置为x:Key,并在DataGrid 上设置CellStyle

例子:

<Style x:Key="MyDataGridCell" TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
    <Setter Property="Background" Value="White"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
    </Style.Triggers>
</Style>

<DataGrid CellStyle="{StaticResource MyDataGridCell}" />

【讨论】:

  • 再一次,我太愚蠢了。下班后我在自己的东西上工作,我总是很累。很抱歉在这里浪费您的时间......我不明白的一件事是默认情况下它没有应用于 CellStyle。对此有任何想法,因为这就是为什么我一开始没有给它钥匙?再次感谢...
  • 如果你只使用TargetType 而不使用Key,该样式将自动应用于该类型的所有元素,除非它们已使用 StaticResource/DynamicResource 声明了样式,我刚刚测试了 DataGrid 及其同样如果我从您的代码中删除CellStyle="{StaticResource DataGridCell}",则应用样式
  • 我将根据这个答案提出另一个问题...如果您能提供更多智慧,我将不胜感激。
  • 这个问题又是我的另一个错误 - 我要睡觉了......再次感谢,非常感谢。
【解决方案2】:

仅将样式应用于某些 DataGridRow :

创建您的 DataGridCell 样式:

< !-- DataGridCell Style-->
< Style x:Key="MyDataGridCellStyle" TargetType="{x:Type DataGridCell}">
    <Setter Property="Background" Value="White"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
    </Style.Triggers>
</Style>

在你想要的列中使用它

< !-- DataGrid -->
<DataGrid >
    <DataGrid.Columns>
        <DataGridComboBoxColumn CellStyle="{StaticResource MyDataGridCellStyle}" />
        <DataGridTextColumn CellStyle="{StaticResource MyDataGridCellStyle}" />
    </DataGrid.Columns>
</DataGrid>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 2022-01-15
    • 2021-01-11
    相关资源
    最近更新 更多