我遇到了动画问题。首先,我尝试使用XAML标记内的VisualStateManager
进行动画制作。这在尝试为Opacity
属性设置动画时起作用,但在Height
属性中没有效果。然后我想我会通过编程动画来试一试,这样我就可以更轻松地进行调试。因此,我不断得到以下内容:
无法解析TargetName ExplorerBody
我不知道为什么动画不透明度有效而不是高度,我不知道为什么它无法解析TargetName。查看我的代码,也许你可以看到我无法看到的东西:
<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>
以下是我尝试使用C#代码完成它的方法:
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();
我的代码出了什么问题?
答案 0 :(得分:1)
您的原始尝试与MS文档匹配 -
Storyboard storyboard = new Storyboard();
storyboard.SetValue(Storyboard.TargetNameProperty, _explorerBody.Name);
storyboard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Opacity"));
DoubleAnimation anim = new DoubleAnimation();
和你的解决方案 -
Storyboard storyboard = new Storyboard();
storyboard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Opacity"));
DoubleAnimation animation = new DoubleAnimation();
storyboard.Children.Add(animation);
Storyboard.SetTarget(animation, _explorerBody);
特别是取代 -
storyboard.SetValue(Storyboard.TargetNameProperty part with -
Storyboard.SetTarget(animation, _explorerBody);
是它的裂缝。
答案 1 :(得分:0)
不确定TargetName错误,但我很确定你需要有类似的东西 (UIElement.Height)用于TargetProperty 现在不能尝试重现你的问题,所以只是猜测......
<DoubleAnimation Storyboard.TargetName="ExplorerBody" Storyboard.TargetProperty="(UIElement.Height)" Duration="0:0:0.5" From="1" To="0" />
答案 2 :(得分:0)
好的,我终于开始工作......以下链接很有帮助:
http://mindfusion.eu/Forum/YaBB.pl?board=diaglite_disc;action=display;num=1278055916
这是我的代码,以防有人需要它:
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 = "-";
}
}