我使用了这段代码,但是单击按钮后,它没有恢复为默认样式。 我在做什么错了?
<Style x:Key="normalImage" TargetType="{x:Type Image}">
<Setter Property="Source" Value="/SM-Works;component/Images/code_101.png"/>
</Style>
<Style x:Key="actionImage" TargetType="{x:Type Image}">
<Setter Property="Source" Value="/SM-Works;component/Images/code_10b.png"/>
</Style>
<Style x:Key="buttonstylesample" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Image x:Name="img" Style="{DynamicResource normalImage}"/>
<Border
SnapsToDevicePixels="True"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}">
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Style" TargetName="img" Value="{DynamicResource actionImage}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Style" TargetName="img" Value="{DynamicResource actionImage}"/>
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Style" TargetName="img" Value="{DynamicResource actionImage}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
MouseOver,IsPressed已激活,但是按下按钮后,按钮图像未恢复正常。
答案 0 :(得分:0)
发生这种情况是由于以下触发器:
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Style" TargetName="img" Value="{DynamicResource actionImage}"/>
</Trigger>
这是怎么回事,当您按下按钮时,键盘焦点也移到了按钮上,因此当您释放鼠标时,仍将应用actionImage
样式。您可以通过将焦点放在按钮上来验证此效果,然后您会看到img
上的样式将重置为normalImage
样式。