I defined ListBox
in my XAML which uses ItemTemplate
.
Inside the ItemTemplate
I placed Image.
<ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel x:Name="itmTempPanel" IsItemsHost="True" ItemWidth="60" ItemHeight="60" Width="{Binding ElementName=lstFilesDropped, Path=Width}"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
...
<Image>
<Image.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Height" To="71" Duration="0:0:0.3" />
<DoubleAnimation Storyboard.TargetName="itmTempPanel" Storyboard.TargetProperty="Height" To="71" Duration="0:0:0.3" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Image.Triggers>
</Image>
</ListBox.ItemTemplate>
When the mouse enter the image I want to begin storyboard on that image height and on the WrapPanel
which I defined inside the ItemsPanelTemplate
.
When the mouse enter to this image I got the following exception: "'itmTempPanel' name cannot be found in the name scope of 'System.Windows.Controls.Image'."
How can I change other element property from element that begin the storyboard.
Thank you for your help !!
There are 2 ways to solve this. The first is using
{x:Reference}
a feature in .NET 4.0 for WPF. You should follow this approach if your application targets .NET 4.0. The idea is setting theStoryboard.Target
to the object you want to animate (in this case is theWrapPanel
). Although we can useBinding
forStoryboard.Target
but we cannot useRelativeSource
orElementName
to set the binding source becauseStoryboard
(or Timeline) is not a FrameworkElement (or FrameworkContentElement). The only way to specify the source is setting theSource
property. However we can normally set this to aStaticResource
orDynamicResource
or directly (using element syntax). It's fortunate that the{x:Reference}
was introduced in .NET 4.0 and this can help you reference any named object inside XAML (the way it works is not likeElementName
). Here is the code for the first approach:The second approach is based on
DataTrigger
. However this trigger is not forImage
, it's exactly for theWrapPanel
. But now theElementName
can be used to bind the Trigger source to theImage
. So this approach is usable when{x:Reference}
is not supported.Note that you have to give the
Image
a name (such asimage
). The<DoubleAnimation>
for the WrapPanel is removed. Instead of usingEventTrigger
, we usedDataTrigger
listening toIsMouseOver
property. You can also specify theDataTrigger.ExitActions
to start animating whenIsMouseOver
is false (equal toMouseLeave
).