我遇到動畫問題。首先,我嘗試使用XAML標記中的VisualStateManager
進行動畫製作。嘗試對Opacity
屬性進行動畫製作時,該功能有效,但不支持Height
屬性。然後我想我會試着通過編程動畫來試試,這樣我就可以更容易地進行調試。因此,我一直得到以下幾點:Silverlight動畫:無法解析目標名稱
無法解析的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();
那麼,什麼是錯我的代碼?
不幸的是,這並沒有幫助。我實際上更喜歡使它在代碼而不是XAML中工作,因爲動畫'Opacity'(在XAML中工作正常)看起來更好,但在這種情況下,我需要處理'Completed'事件,並且我更喜歡在內部處理它我的自定義控件的類(不在視圖模型上)。因此,你有什麼想法爲什麼我得到這個錯誤? – Kassem
好吧,即使故事板及其觸發器是在xaml中定義的,也不會阻止您在控件中處理完成的事件。 您也可以將可見性設置爲Collapsed,並使用DoubleAnimationUsingKeyframe更改動畫結尾的按鈕的content屬性。 在這種情況下,最好的解決方案是利用國家,讓你的故事板從一種狀態變爲另一種狀態。 只要動畫變得更復雜,將故事板全部寫入代碼就會變得毛茸茸。 –