[英]WPF Button - Specifying Template and Style together
I have a button: 我有一个按钮:
<Button x:Name="someButton"
Template="{StaticResource aButtonTemplate}"
Command="{Binding MyCommand}"
Style="{StaticResource ButtonStyle1}" >
Now, the aButtonTemplate looks like: 现在, aButtonTemplate看起来像:
<ControlTemplate x:Key="aButtonTemplate" TargetType="{x:Type Button}">
<Border Name="border" Background="Transparent" BorderBrush="Transparent" BorderThickness="2">
<ContentPresenter x:Name="content" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
Then I have the ButtonStyle1 然后我有ButtonStyle1
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Viewbox>
<Grid>
<Ellipse Stroke="#f2f2f0" StrokeThickness="3" Fill="red" Width="77" Height="77"/>
</Grid>
</Viewbox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Now the problem is, due to the line Template="{StaticResource aButtonTemplate}"
the Style ButtonStyle1 is not getting applied. 现在的问题是,由于出现了行Template="{StaticResource aButtonTemplate}"
因此样式ButtonStyle1没有得到应用。 I am not seeing any Ellipse. 我没有看到任何椭圆形。
How can I get the Style to work along with the Template ? 如何使样式与模板一起使用?
EDIT: As I am specifying the ControlTemplate again in the Style ButtonStyle1, is there a way to combine both the styles into ButtonStyle2 using BasedOn? 编辑:当我再次在样式ButtonStyle1中指定ControlTemplate时,是否有一种方法可以使用BasedOn将两种样式组合成ButtonStyle2? How can I do it? 我该怎么做?
<Style x:Key="StyleButton2"
TargetType="Button"
BasedOn="{StaticResource StyleButton1}">
<Setter Property="Template"
Value="{StaticResource aButtonTemplate}"/>
</Style>
Then use just this style on the Button instance. 然后在Button实例上仅使用此样式。
Obviously, have both StyleButton1 and aButtonTemplate defined before this style(cause the use of StaticResource). 显然,在此样式之前定义了StyleButton1和aButtonTemplate(因为使用了StaticResource)。
How can I get the Style to work along with the Template? 如何使样式与模板一起使用?
The are both being applied but the Template
property that you set to {StaticResource aButtonTemplate}
overrides the value of the Template
property that the Style
sets. 将都被应用,但Template
,你设置属性{StaticResource aButtonTemplate}
覆盖的值Template
属性的Style
集。 Local values takes precedence over values set by style setters: https://msdn.microsoft.com/en-us/library/ms743230(v=vs.110).aspx . 本地值优先于样式设置器设置的值: https ://msdn.microsoft.com/zh-cn/library/ms743230( v= vs.110) .aspx 。
EDIT: As I am specifying the ControlTemplate again in the Style ButtonStyle1, is there a way to combine both the styles into ButtonStyle2 using BasedOn? 编辑:当我再次在样式ButtonStyle1中指定ControlTemplate时,是否有一种方法可以使用BasedOn将两种样式组合成ButtonStyle2? How can I do it? 我该怎么做?
You can base a Style
on another Style
but you cannot base a ControlTemplate
on another ControlTemplate
. 您可以将Style
基于另一个Style
但是不能将ControlTemplate
放置在另一个ControlTemplate
。 A ControlTemplate
must be defined as a whole: 必须将ControlTemplate
整体定义:
WPF: Is there a way to override part of a ControlTemplate without redefining the whole style? WPF:有没有一种方法可以重写ControlTemplate的一部分而不重新定义整个样式?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.