我有一个ToggleButton,里面有一个画布,握着一个椭圆形。
<ToggleButton IsChecked="{Binding IsUpgradeOpen, Mode=OneWayToSource}"
x:Name="upgradeButton"
Click="upgradeButton_Click" Background="Transparent">
....
....
<Canvas Name="svg4203pro" Width="40" Height="40">
....
<Ellipse xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="patph41591" Fill="#FF494949">
在选中ToggleButton时,我想更改椭圆的颜色。
我想不出一种可行的方法。
尝试1。
<ToggleButton IsChecked="{Binding IsUpgradeOpen, Mode=OneWayToSource}"
x:Name="upgradeButton"
Click="upgradeButton_Click" Background="Transparent">
<ToggleButton.Style>
<Style>
<Style.Triggers>
<Trigger Property="ToggleButton.IsChecked" Value="true" >
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="patph41591" Storyboard.TargetProperty="(Ellipse.Fill).Color">
<DiscreteColorKeyFrame KeyTime="0:0:0" Value="Red"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
这给了我错误TargetName property cannot be set on a Style Setter.
,这是我没有想到的,因为我能够对鼠标悬停效果进行类似的操作(我可以使鼠标悬停效果达到如此轻松地工作,这对我来说并不困惑。
这适用于鼠标悬停
<ToggleButton.Triggers>
<EventTrigger RoutedEvent="Button.MouseEnter">
<BeginStoryboard>
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="patph41591" Storyboard.TargetProperty="(Ellipse.Fill).Color">
<DiscreteColorKeyFrame KeyTime="0:0:0" Value="#6dbd63"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
尝试2。
尝试从Ellipse本身而不是父ToggleButton更改样式
<Ellipse xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Canvas.Left="0" Canvas.Top="1012.3" Width="40" Height="40" Name="patph41591"
Fill="#FF494949" StrokeThickness="7.80000019" StrokeMiterLimit="4" StrokeLineJoin="Round" StrokeStartLineCap="Square"
StrokeEndLineCap="Square" Opacity="1">
<Ellipse.Style>
<Style TargetType="{x:Type Ellipse}">
<Style.Triggers>
<DataTrigger
Binding="{Binding Path=IsUpgradeOpen}"
Value="True">
<!--RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}-->
<Setter Property="Fill" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Ellipse.Style>
</Ellipse>
这使用了DataContext属性IsUpgradeOpen,该属性绑定到ToggleButton.IsClicked,但是它什么都不做。没有绑定错误,什么都没有。
没有其他样式分配给ToggleButton。