我是 WinRT/XAML 开发新手。经过数小时的网络研究和多次反复试验,我仍然无法理解如何使用 VisualStateManager 根据用户对对象的输入来更改椭圆的填充颜色。以下代码不起作用。这是今天的代码:
<Ellipse Stroke="White" StrokeThickness="5" Width="90" Height="90">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation To="Red" Storyboard.TargetName="Ellipse" Storyboard.TargetProperty="Fill.Color"/>
</Storyboard>
</VisualState>
<VisualStateGroup.Transitions>
<VisualTransition To="Normal" GeneratedDuration="00:00:01"/>
<VisualTransition To="MouseOver" GeneratedDuration="00:00:01"/>
</VisualStateGroup.Transitions>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Ellipse>
更新:
感谢 Nicholas W. 在正确方向上的推动。我错过了模板以及正确的目标属性。以下代码按预期工作:
<Button>
<Button.Template>
<ControlTemplate>
<Grid>
<Ellipse x:Name="myEllipse" Stroke="White" StrokeThickness="5" Width="70" Height="70" Fill="Transparent"/>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimation Storyboard.TargetName="myEllipse" To="#FF0061D4" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Duration="0:0:0"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>