我正在尝试在 App.xaml 中定义全局按钮样式,它基本上按我的预期工作。但是,我只是不知道如何让前台正常工作。无论我做什么,我都会获得默认 TextBlock 的样式(它将颜色设置为白色)。
<Style TargetType="{x:Type Button}">
<Setter Property="Margin" Value="3, 5" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="FocusVisualStyle"
Value="{StaticResource ButtonFocusVisual}" />
<Setter Property="Foreground" Value="Red" />
<Setter Property="Padding" Value="5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="gridMainButton"
RenderTransformOrigin="0.5, 0.5">
<Grid.RenderTransform>
<ScaleTransform x:Name="scaleTransform"
CenterX="0.5"
CenterY="0.5" />
</Grid.RenderTransform>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates" >
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Pressed">
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="scaleTransform"
Storyboard.TargetProperty="ScaleX"
Duration="0"
To="0.85" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Ellipse x:Name="ellipse"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
StrokeThickness="2"
Stroke="{StaticResource standardBackground}"
Fill="{StaticResource standardBackground}" />
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="4, 8"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我想我可以将 ContentPresenter 更改为 TextBlock,这对于这个特定的应用程序来说没问题,但我正在寻找更通用的解决方案。
Thanks,
wTs
就像 Markus Hütter 所说,问题可能是你有一个隐式的样式TextBlock
定义并且当Button Content
被设置为一个字符串,aTextBlock
将在您拥有的地方创建ContentPresenter
在模板中。这TextBlock
将拾取隐式样式,这就是您遇到此问题的原因。
您可以禁用隐式样式TextBlock
通过指定 a 为字符串创建的 sDataTemplate
for string
。将以下内容添加到 App.xaml
<Application ...>
<Application.Resources>
<DataTemplate xmlns:sys="clr-namespace:System;assembly=mscorlib"
DataType="{x:Type sys:String}">
<TextBlock Text="{Binding}">
<TextBlock.Resources>
<Style TargetType="{x:Type TextBlock}"/>
</TextBlock.Resources>
</TextBlock>
</DataTemplate>
<!--...-->
</Application.Resources>
</Application>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)