WPF全局设置DataGrid内部的TextBlock样式 [英] WPF globally styling a TextBlock inside a DataGrid
问题描述
我遇到一个很奇怪的问题。我试图将全局样式应用于 DataGrid
中的多个控件。他们中的大多数人的工作完全符合我的期望。但是, TextBlock
的样式永远不会应用。 ComboBox
, TextBox
, Label
和其他几种样式所有这些都将应用于其各自的控件,而不是 TextBlock
。我已尽可能简化了代码,问题仍然存在。我已经在下面提供了代码示例。
I am encountering a very weird issue. I am trying to apply global styling to several controls within a DataGrid
. Most of them work exactly how I would expect them to. However, the styling for the TextBlock
never gets applied. Styles for ComboBox
, TextBox
, Label
, and several others all are getting applied to their respective controls, but not the TextBlock
. I have simplified the code as much as possible and the issue is still present. I have provided the code sample below.
我需要将样式应用于 TextBlock
,但是我没有不想手动将其应用于 TextBlock
。
I need the style to be applied to the TextBlock
and I don't want to have to apply it manually to the TextBlock
.
<DataGrid ItemsSource="{Binding Data}" AutoGenerateColumns="False">
<DataGrid.Resources>
<Style TargetType="TextBlock">
<Setter Property="ANY_TEXTBLOCK_PROPERTY" Value="VALUE" />
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Test">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="Globably Applied" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
更多信息:
- 除
TextBlock
(TextBox
,ComboBox
等)正常工作。 - 在
DataTemplate
中定义全局样式将会正常工作。 - 使用
x:Key $ c将样式直接分配给
TextBlock
$ c>将起作用。 - 使用
TextElement.PROPERTY
DataGridCell 的全局样式>将应用于TextBlock
。
- Global styles for any control other than
TextBlock
(TextBox
,ComboBox
, etc.) work properly. - Defining the global style inside the
DataTemplate
will work properly. - Directly assigning the style to the
TextBlock
using anx:Key
will work. - Global styles for
DataGridCell
usingTextElement.PROPERTY
will get applied to aTextBlock
.
其中一些将得到应用于 TextBlock
的样式,他们有自己的问题。直接分配样式或在 DataGridColumn
内的某个位置定义样式将意味着我将不得不多次应用样式。在 DataGridCell
上使用 TextElement.PROPERTY
会将样式应用于不仅仅是 TextBlock
进行控制,并会限制您可以设置的属性数量。
While some of these will get the style applied to the TextBlock
, they have there own issues. Directly assigning the style or defining the style somewhere within a DataGridColumn
will mean that I will have to apply the style more than once. Using the TextElement.PROPERTY
on the DataGridCell
will apply the style to more than just TextBlock
controls and will limit the number of properties that you can set.
推荐答案
因此,我们需要进行更多的挖掘幸运的是,我发现WPF不会在模板内应用隐式样式,除非 TargetType
来自 Control
。由于 TextBlock
并非源自 Control
,因此不会应用其样式。因此,您必须手动将样式应用于每个非 Control
或在模板内部定义隐式样式。
So with a bit more digging and a little luck, I discovered that WPF does not apply implicit styles inside templates unless the TargetType
derives from Control
. Since TextBlock
doesn't derive from Control
, its style is not applied. So you either have to manually apply the style to every non-Control
or define the implicit style inside the template.
以下MSDN博客文章对此进行了非常详细的说明。
The following MSDN blog post explains it in pretty good detail.
https://docs.microsoft.com/zh-CN/archive/blogs/wpfsdk/implicit-styles-templates-controls-和框架元素
这篇关于WPF全局设置DataGrid内部的TextBlock样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!