好吧,您可以使用附加属性来做到这一点。您可以在单独的类中定义它们(例如,ButtonHelper
),就像这里描述的那样:http: //msdn.microsoft.com/en-us/library/ms749011.aspx#custom。
接下来,让您的样式引用附加的属性:
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<StackPanel Orientation="Horizontal" >
<Image Name="PART_Image"
Source="{Binding (local:ButtonHelper.NormalImageSource),
RelativeSource={RelativeSource Self}}" />
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source"
Value="{Binding (local:ButtonHelper.HoverImageSource),
RelativeSource={RelativeSource Self}}"
TargetName="PART_Image"/>
</Trigger>
<!-- and so on -->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
(代码是从这个答案中窃取的)。
不要忘记声明为类local
所在的命名空间ButtonHelper
!