在下面的代码中,我要在鼠标悬停时将蓝色图像背景按钮更改为绿色图像按钮。
我的XAML代码在下面,但是将鼠标悬停在按钮上没有任何反应。NextButton.png
是原始按钮,nextGreen.png
应该是鼠标悬停时的背景。
问题出在哪儿 ?
<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
Content=""
HorizontalAlignment="Right"
Margin="0,0,0.2,0"
VerticalAlignment="Bottom"
Width="50"
Height="100"
Grid.Column="1"
Click="Next_Click">
<Button.Background>
<ImageBrush ImageSource="NextButton.png" />
</Button.Background>
<Button.Template>
<ControlTemplate>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="nextGreen.png" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
<StackPanel>
<StackPanel.Background>
<ImageBrush ImageSource="Nextbutton.png"/>
</StackPanel.Background>
</StackPanel>
</ControlTemplate>
</Button.Template>
</Button>
您设置了按钮的背景,但随后覆盖了按钮,Template
因此Button.Background
永不显示。您应该删除,Button.Background
然后在StackPanel
中命名,Template
以便您可以更改中的背景Trigger
。
<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Content="" HorizontalAlignment="Right" Margin="0,0,0.2,0" VerticalAlignment="Bottom" Width="50" Height="100" Grid.Column="1" Click="Next_Click">
<Button.Template>
<ControlTemplate>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="root">
<Setter.Value>
<ImageBrush ImageSource="nextGreen.png" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
<StackPanel x:Name="root">
<StackPanel.Background>
<ImageBrush ImageSource="Nextbutton.png"/>
</StackPanel.Background>
</StackPanel>
</ControlTemplate>
</Button.Template>
</Button>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句