WPF中Button的样式重写后的focus的样式问题
今天在开发中遇到这样一个问题,先看下面的样式:
<Style x:Key="MyButton" TargetType="{x:Type Button}">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="Width" Value="96" />
<Setter Property="Height" Value="96" />
<Setter Property="Content" Value="hheee" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="Border" BorderThickness="0">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Blue" />
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
标红这段代码是设置focus时的背景颜色。
这里就出现问题了,我们在style里面重写了controltemplate,这里的设置就有问题了。背景颜色不变化。
解决的方法有两个,将标红的triggers放到controltemplate里面或者在controltemplate 里面的border上加上这样一句 Background="{TemplateBinding Panel.Background}"
问题就解决了。
这里主要是我们重写了ControlTemplate后,并没有将重写后的内容和事件直接进行Binding。
Thanks
Sailor Lee