Reputation: 8266
I'm having issues with animations. First I tried animating using the VisualStateManager
from within the XAML mark-up. That worked when trying to animate the Opacity
property, but not the Height
property. Then I thought I'd give it a try by animating programmatically so I could debug more easily. Accordingly, I kept getting the following:
Cannot resolve TargetName ExplorerBody
I donno why animating the opacity works but not the height, and I do not know why it cannot resolve the TargetName. Checkout my code, maybe you can see something that I couldn't:
<ControlTemplate TargetType="Controls:ApplicationExplorer">
<Border BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderBrush}"
BorderThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness}"
CornerRadius="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CornerRadius}"
Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Width}"
Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Height}" >
<Grid x:Name="Root" MinWidth="100">
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HeaderBackgroundBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="1" Text="{TemplateBinding Title}" Style="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TitleStyle}" VerticalAlignment="Center" />
<Controls:LayoutToggleButton Grid.Column="2" x:Name="LayoutButton" Cursor="Hand" />
</Grid>
<Border x:Name="ExplorerBody" Background="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}"
Grid.Row="1" HorizontalAlignment="Stretch" BorderThickness="0" >
<toolkit:TreeViewDragDropTarget AllowedSourceEffects="Copy" x:Name="treeViewDropTarget"
HorizontalAlignment="Left" VerticalAlignment="Top" >
<sdk:TreeView x:Name="treeView" ItemsSource="{TemplateBinding Nodes}" Background="Transparent" BorderThickness="0"
ItemTemplate="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TreeItemTemplate}"
HorizontalAlignment="Left" Margin="10 0 0 0" />
</toolkit:TreeViewDragDropTarget>
</Border>
</Grid>
<vsm:VisualStateManager.VisualStateGroups>
<vsm:VisualStateGroup x:Name="LayoutGroup">
<vsm:VisualState x:Name="Minimized">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="ExplorerBody" Storyboard.TargetProperty="Height" Duration="0:0:0.5" From="1" To="0" />
</Storyboard>
</vsm:VisualState>
<vsm:VisualState x:Name="Maximized" />
</vsm:VisualStateGroup>
</vsm:VisualStateManager.VisualStateGroups>
</Border>
</ControlTemplate>
And here's how I tried to get it done using C# code:
Storyboard storyboard = new Storyboard();
storyboard.SetValue(Storyboard.TargetNameProperty, _explorerBody.Name);
storyboard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Opacity"));
DoubleAnimation anim = new DoubleAnimation();
anim.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 500));
anim.To = 0;
storyboard.Children.Add(anim);
storyboard.Begin();
So what's wrong with my code?
Upvotes: 1
Views: 3149
Reputation: 350
Your original attempt matches the MS documentation -
Storyboard storyboard = new Storyboard();
storyboard.SetValue(Storyboard.TargetNameProperty, _explorerBody.Name);
storyboard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Opacity"));
DoubleAnimation anim = new DoubleAnimation();
and your solution -
Storyboard storyboard = new Storyboard();
storyboard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Opacity"));
DoubleAnimation animation = new DoubleAnimation();
storyboard.Children.Add(animation);
Storyboard.SetTarget(animation, _explorerBody);
In particular replacing the -
storyboard.SetValue(Storyboard.TargetNameProperty part with -
Storyboard.SetTarget(animation, _explorerBody);
is what cracks it.
Upvotes: 1
Reputation: 8266
Ok I finally got it to work... The following link was helpful:
http://mindfusion.eu/Forum/YaBB.pl?board=diaglite_disc;action=display;num=1278055916
Here's my code in case someone needs it:
private void SwitchLayoutState(object sender, RoutedEventArgs e)
{
if (_currentState == ControlLayout.None)
{
throw new ArgumentException("Control layout cannot be None.");
}
Duration duration = new Duration(new TimeSpan(0, 0, 0, 0, 400));
Storyboard storyboard = new Storyboard();
storyboard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Opacity"));
DoubleAnimation animation = new DoubleAnimation();
storyboard.Children.Add(animation);
Storyboard.SetTarget(animation, _explorerBody);
if (_currentState == ControlLayout.Maximized)
{
animation.To = 0;
storyboard.Completed += (s, args) => { _explorerBody.Visibility = System.Windows.Visibility.Collapsed; };
storyboard.Begin();
_currentState = ControlLayout.Minimized;
_layoutButton.Content = "+";
}
else if (_currentState == ControlLayout.Minimized)
{
_explorerBody.Visibility = System.Windows.Visibility.Visible;
animation.To = 1;
storyboard.Begin();
_currentState = ControlLayout.Maximized;
_layoutButton.Content = "-";
}
}
Upvotes: 0
Reputation: 11864
Not sure about the TargetName error, but I'm pretty sure you need to have something like
(UIElement.Height) for the TargetProperty.
Can't try to reproduce your issue now, so just a wild guess...
<DoubleAnimation Storyboard.TargetName="ExplorerBody" Storyboard.TargetProperty="(UIElement.Height)" Duration="0:0:0.5" From="1" To="0" />
Upvotes: 0