问题描述
我仍在与MvvM和Prism一起学习WPF和XAML。
我正在将MahApps与Prism一起使用,并且在按钮中嵌入了一个图标。每当鼠标悬停在图标上时,我都希望在该图标上调用“ Spin”方法。
我该怎么做?
下面是代码:
<Grid Background="#52514e">
<StackPanel HorizontalAlignment="Left" Orientation="Vertical">
<CheckBox Content="CanExecute" IsChecked="{Binding CanExecute}"></CheckBox>
<Button x:Name="TestButton" Command="{Binding ClickCommand}" Background="#52514e" Height="35" Width="35" BorderThickness="0" ToolTip=" Marks the current work order as complete.">
<Button.Style>
<Style targettype="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate targettype="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Gray"></Setter>
</Trigger>
<Trigger Property="Button.IsMouSEOver" Value="True">
<Setter Property="Foreground" Value="Yellow"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
<iconPacks:Unicons Width="35" Height="35" Kind="Cog" Spin="True"/>
</Button>
<TextBlock Text="{Binding Title}"></TextBlock>
</StackPanel>
</Grid>
解决方法
iconPacks:Unicons
元素是标记扩展,而不是控件。改为使用PackIconUnicons
控件,并将Spin
属性绑定到父按钮的IsMouseOver
属性。
<iconPacks:PackIconUnicons Width="35" Height="35" Kind="Cog" Spin="{Binding IsMouseOver,RelativeSource={RelativeSource AncestorType={x:Type Button}}}"/>