I have a grid inside the DataCemplate ItemsControl, so there will be many copies of this grid. I want to click on the grid to invoke the storyboard of an element outside the DataTemplate. For example, I would like to animate the Transform properties of a named element that exist elsewhere in 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:
Name'myRectangle' cannot be found in the namespace 'System.Windows.Controls.Grid'.
, ? xaml-only, .