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?