Wpf 如何在动画期间使框架元素保持在中间?
我尝试制作一个位于窗口顶部中心的工具栏 在运行期间,工具栏的宽度可能会更改。我希望借助视觉状态(对我来说非常实用)和动画(使用关键帧)触发宽度 为了尽可能简化问题,我在窗口中放置了一个DockPanel,其中包含:Wpf 如何在动画期间使框架元素保持在中间?,wpf,Wpf,我尝试制作一个位于窗口顶部中心的工具栏 在运行期间,工具栏的宽度可能会更改。我希望借助视觉状态(对我来说非常实用)和动画(使用关键帧)触发宽度 为了尽可能简化问题,我在窗口中放置了一个DockPanel,其中包含: 包含两个按钮(从一种视觉状态切换到另一种视觉状态)的堆叠面板 将由添加到其ControlTemplate的两个VisualState设置动画的按钮 这里是这个窗口的截图 Xaml代码: <Window xmlns="http://schemas.microsoft.com/wi
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:is="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="DockPanelIssue.MainWindow"
Background="LightYellow"
Title="MainWindow" Height="200" Width="400">
<DockPanel Background="Blue" HorizontalAlignment="Center" VerticalAlignment="Top">
<StackPanel Margin="5" DockPanel.Dock="Bottom" Background="Orange" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Margin="5" Click="Show_Click" Content="Show"/>
<Button Margin="5" Click="Hide_Click" Content="Hide"/>
</StackPanel>
<Button Margin="5" x:Name="theButton" Background="Yellow" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<ContentControl>
<VisualStateManager.CustomVisualStateManager>
<is:ExtendedVisualStateManager />
</VisualStateManager.CustomVisualStateManager>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup is:ExtendedVisualStateManager.UseFluidLayout="True">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:3"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="StateHide">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="Chrome">
<DoubleKeyFrameCollection>
<LinearDoubleKeyFrame KeyTime="0%" Value="200"/>
<LinearDoubleKeyFrame KeyTime="50%" Value="150"/>
<LinearDoubleKeyFrame KeyTime="100%" Value="100"/>
</DoubleKeyFrameCollection>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="StateShow">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="Chrome">
<DoubleKeyFrameCollection>
<LinearDoubleKeyFrame KeyTime="0%" Value="100"/>
<LinearDoubleKeyFrame KeyTime="50%" Value="150"/>
<LinearDoubleKeyFrame KeyTime="100%" Value="200"/>
</DoubleKeyFrameCollection>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ContentControl>
</ControlTemplate>
</Button.Template>
Animated Button
</Button>
</DockPanel>
我们期待的是AnimatedButton(黄色的)在3秒内增加他的宽度,(并且停留在中间)。但结果是:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Hide_Click(object sender, RoutedEventArgs e)
{
ExtendedVisualStateManager.GoToState(theButton, "StateHide", true);
}
private void Show_Click(object sender, RoutedEventArgs e)
{
ExtendedVisualStateManager.GoToState(theButton, "StateShow", true);
}
}