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
How to Apply a Cell Style to DataGrid Cell
提问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 DynamicResource
but 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 Style
has no Key
you do not have to set CellStyle
on the DataGrid
, it will be applied to all DataGridCell
by default.
因为你Style
没有Key
,你不必设置CellStyle
上DataGrid
,它会被应用到所有DataGridCell
默认。
If you dont want it applied to all DataGridCell
by default give the style an x:Key
and set the CellStyle
on the DataGrid
如果你不希望它适用于所有DataGridCell
默认给的风格x:Key
,并设置CellStyle
在DataGrid
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>