默认 TextBlock 样式覆盖按钮文本颜色
我的问题发生在 .NET 3.5 SP1 中的 WPF 中,可以描述如下:
我有一个默认的 Style
命中 UI 中的所有 TextBlock
元素。这就是它的样子:
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextTrimming" Value="CharacterEllipsis"/>
<Setter Property="Foreground" Value="Red"/>
</Style>
这对所有 TextBlock
都适用。除此之外,我还有一个 Button
样式,包括一个 ControlTemplate
,看起来像这样(缩短):
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}" BasedOn="{x:Null}">
<Setter Property="Foreground" Value="Green"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Border"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}"
Height="24"
BorderBrush="{TemplateBinding BorderBrush}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
TextBlock.Foreground="{TemplateBinding Foreground}"/>
</Border>
<ControlTemplate.Triggers>...</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
注意行 TextBlock.Foreground="{TemplateBinding Foreground}"
在 ContentPresenter
中。这应该将按钮文本设置为绿色,事实上它在 Visual Studio 的设计器视图中也是如此。但是当我编译并运行程序时,按钮文本是红色的,文本颜色由默认的 TextBlock
样式设置。我和史努比验证了这一点。
如何防止默认 TextBlock
样式覆盖 TextBlock.Foreground
值? ContentPresenter
的 OverridesDefaultStyle
属性在这种情况下没有帮助。
有什么想法吗?
My problem occurs with WPF in .NET 3.5 SP1 and can be described as follows:
I have a default Style
hitting all TextBlock
elements in my UI. That is how it looks:
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextTrimming" Value="CharacterEllipsis"/>
<Setter Property="Foreground" Value="Red"/>
</Style>
That works fine for all TextBlock
s. In addition to that I have a Button
style including a ControlTemplate
that looks like this (shortened):
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}" BasedOn="{x:Null}">
<Setter Property="Foreground" Value="Green"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Border"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}"
Height="24"
BorderBrush="{TemplateBinding BorderBrush}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
TextBlock.Foreground="{TemplateBinding Foreground}"/>
</Border>
<ControlTemplate.Triggers>...</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Notice the line TextBlock.Foreground="{TemplateBinding Foreground}"
in the ContentPresenter
. This should set the button text to green and in fact it does in the designer view of Visual Studio. But when I compile and run the program the button text is red, the text color is set by the default TextBlock
style. I verified this with Snoop.
How can I prevent the defaultTextBlock
style from overriding the TextBlock.Foreground
value? The OverridesDefaultStyle
property of ContentPresenter
doesn't help in this case.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅此链接的答案 5
尝试将其添加到 ResourceDictionary
See answer 5 at this link
Try adding this to the ResourceDictionary
您最好不要覆盖 TextBlock 的默认样式。到目前为止,我能想到的最好的想法是为 Control 创建一种样式并将其应用到所有顶级窗口。
请参阅此处了解更多详细信息:http://www.ikriv.com/dev/dotnet/wpftextstyle/
You are better off not overriding default style for the TextBlock. The best idea I could come up with so far is to create a style for Control and apply it to all top level windows.
See here for more details: http://www.ikriv.com/dev/dotnet/wpftextstyle/
关于两个选项:
我建议采用替代方法并使用附加的依赖属性,例如
然后使用命名空间:
创建默认样式:
设置附加属性:
然后,如果您需要获取默认值,则可以在 style:中或直接在按钮上
With respect to the 2 options:
I would suggest an alternative approach and use an attached Dependency Property e.g.
Then having the namespace:
make a default style:
Then if you need to get the default you can just set the attached property either in a style:
or directly on a button: