WPF:在动态创建的控件上按名称从 EventTrigger 触发元素上的动画
我在 ItemsControl 的 DataTemplate 中有一个网格,因此该网格会有许多副本。 我想要在网格上单击鼠标来触发 DataTemplate 外部元素的故事板。 例如,我想对窗口中其他位置存在的命名元素的 Transform 属性进行动画处理。
假设我的 DataTemplate 看起来像这样:
<DataTemplate x:Key="myDataTemplate">
<Grid>
<Grid.Triggers>
<EventTrigger RoutedEvent="UIElement.MouseLeftButtonUp">
<BeginStoryboard>
<Storyboard Storyboard.TargetName="myRectangle">
<DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[1].(TranslateTransform.X)"
To="10" Duration="0:0:0.2" />
<DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[1].(TranslateTransform.Y)"
To="10" Duration="0:0:0.2" />
<DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
To="1" Duration="0:0:0.5" />
<DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
BeginTime="0:0:0.5" To="1" Duration="0:0:0.5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
</Grid>
</DataTemplate>
在我的窗口中的某个地方,我有一个看起来像这样的 Rectangle:
<Rectangle x:Name="myRectangle" Height="400" Width="400">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="0" ScaleY="0" />
<TranslateTransform />
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
当 MouseLeftButtonUp 事件触发时,我收到以下异常:
在 'System.Texture' 的名称范围中找不到 'myRectangle' 名称。 Windows.Controls.Grid'。
有没有办法告诉故事板在哪里寻找这个名称的元素? 我对仅 xaml 的解决方案特别感兴趣(如果存在)。
I have a Grid inside of an ItemsControl's DataTemplate, so there will be many copies of this grid. I want a mouse click on the grid to trigger the storyboard of an element outside of the DataTemplate. For example, I would like to animate Transform properties of a named element that exists elsewhere in the Window.
Let's say my DataTemplate looks something like this:
<DataTemplate x:Key="myDataTemplate">
<Grid>
<Grid.Triggers>
<EventTrigger RoutedEvent="UIElement.MouseLeftButtonUp">
<BeginStoryboard>
<Storyboard Storyboard.TargetName="myRectangle">
<DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[1].(TranslateTransform.X)"
To="10" Duration="0:0:0.2" />
<DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[1].(TranslateTransform.Y)"
To="10" Duration="0:0:0.2" />
<DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
To="1" Duration="0:0:0.5" />
<DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
BeginTime="0:0:0.5" To="1" Duration="0:0:0.5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
</Grid>
</DataTemplate>
and somewhere in my window I have a Rectangle that looks like this:
<Rectangle x:Name="myRectangle" Height="400" Width="400">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="0" ScaleY="0" />
<TranslateTransform />
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
When the MouseLeftButtonUp event fires, I get the following exception:
'myRectangle' name cannot be found in the name scope of 'System.Windows.Controls.Grid'.
Is there a way to tell the storyboard where to look for an element by this name? I'm especially interested in a xaml-only solution if one exists.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要您尝试绑定的 xaml 元素是父元素,就可以使用relativesource 绑定的 FindAncestor 模式在不同的名称范围(即数据模板的外部)中查找 xaml 元素。 绑定代码如下所示:
Storyboard 中的名称绑定可能是完全不同的动物,但也许您可以使用它。
It's possible to find xaml elements in a different namescope (i.e. external of the data template) by using the FindAncestor mode of a RelativeSource binding, as long as the xaml element you're trying to bind to is a parent element. The binding code would look like:
Name bindings in storyboards may be a different animal entirely, but perhaps you can use this.