C# 如何将单元格样式应用于 DataGrid 单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18623579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:46:42  来源:igfitidea点击:

How to Apply a Cell Style to DataGrid Cell

c#wpfdatagridstyles

提问by MoonKnight

I have the following DataGrid

我有以下 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>

I have the following cell style to change the selected Backcolor

我有以下单元格样式来更改选定的 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>

I have tried applying the CellStyle="{StaticResource DataGridCell}"as shown above, and using DynamicResourcebut the resource is failing to be resolved. I have imported the correct resource dictionary as other styles are working What am I doing wrong here?

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

Thanks for your time.

谢谢你的时间。

采纳答案by sa_ddam213

Since your Stylehas no Keyyou do not have to set CellStyleon the DataGrid, it will be applied to all DataGridCellby default.

因为你Style没有Key,你不必设置CellStyleDataGrid,它会被应用到所有DataGridCell默认。

If you dont want it applied to all DataGridCellby default give the style an x:Keyand set the CellStyleon the DataGrid

如果你不希望它适用于所有DataGridCell默认给的风格x:Key,并设置CellStyleDataGrid

Example:

例子:

<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}" />

回答by Mr Rubix

To apply the style to only some DataGridRow :

将样式仅应用于某些 DataGridRow :

Create your DataGridCell style :

创建您的 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>

Use it in the column you want

在您想要的列中使用它

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