当您试图为某些DataTemplate
输入VisualStates
时,这是很常见的问题。使用VisualStateManager的数据模板中的WPF动画
下面的代码工作正常,但只有当我使用FrameworkElement
,如自定义UserControl
:
<UserControl>
...namespaces goes here...
<Grid x:Name="rootgrid">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="States">
<Storyboard x:Key="Focused">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="ellipse">
<EasingColorKeyFrame KeyTime="0:0:0.1" Value="#FFE90B0B"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Ellipse x:Name="ellipse"
Width="26"
Height="26"
Fill="Yellow"
SnapsToDevicePixels="True"
Stretch="Fill"
Stroke="Black"
StrokeThickness="2">
<Ellipse.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Ellipse.RenderTransform>
</Ellipse>
</Grid>
</UserControl>
但是,当我尝试将代码粘贴在DataTemplate
:
<DataTemplate x:Key="MyDataTemplate">
<Grid x:Name="rootgrid">
... Code the same as above...
</Grid>
</DataTemplate>
然后我将“MyDataTemplate”应用于我的自定义元素(实现ContentTemplate
依赖项属性的类),并且在这种情况下我无法使用动画状态“聚焦”。
即使我会得到一个名为 “rootgrid” 网格物体通过的VisualTree并使用此:
VisualStateManager.GoToState(rootgrid,"Focused",true);
什么也没有发生... :(
的问题是如何使用VisualStates
(动画)实施?在DataTemplate
非FrameworkElement
对象
你找到解决这个?我想知道同样的事情。也许它不可能使用数据模板的可视状态? – McGarnagle
也许我完全错了,但我认为有没有简单的方法来使用像Ellipse这样的UIElement。所以你应该可以使用从Control继承的类,并看看教程或文章之一如何使用它们。这就是我可以建议的,对不起。其实我没有太多的WPF经验,所以也许有人会告诉我们如何做到最好。 –