wpf - 基于 WPF 中的绑定开始 Storyboard
问题描述
我正在尝试在我的 wpf 应用程序中创建一个类似汉堡包的 Windows 10 按钮。现在一切都很好,但我找不到不使用后面的代码来启动动画的方法。到目前为止,这是我的 XAML:
<UserControl.Resources>
<Storyboard x:Key="OpenMenu">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"
Storyboard.TargetName="rectangle">
<EasingDoubleKeyFrame KeyTime="0"
Value="0" />
<EasingDoubleKeyFrame KeyTime="0:0:0.3"
Value="43.5" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"
Storyboard.TargetName="rectangle1">
<EasingDoubleKeyFrame KeyTime="0"
Value="0" />
<EasingDoubleKeyFrame KeyTime="0:0:0.3"
Value="-43.5" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
Storyboard.TargetName="rectangle2">
<EasingDoubleKeyFrame KeyTime="0"
Value="1" />
<EasingDoubleKeyFrame KeyTime="0:0:0.3"
Value="0" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)"
Storyboard.TargetName="GridMenu">
<EasingDoubleKeyFrame KeyTime="0"
Value="40" />
<EasingDoubleKeyFrame KeyTime="0:0:0.3"
Value="200" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Opacity)"
Storyboard.TargetName="Title">
<EasingDoubleKeyFrame KeyTime="0"
Value="0" />
<EasingDoubleKeyFrame KeyTime="0:0:0.3"
Value="1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="CloseMenu">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"
Storyboard.TargetName="rectangle">
<EasingDoubleKeyFrame KeyTime="0"
Value="43.5" />
<EasingDoubleKeyFrame KeyTime="0:0:0.3"
Value="0" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"
Storyboard.TargetName="rectangle1">
<EasingDoubleKeyFrame KeyTime="0"
Value="-43.5" />
<EasingDoubleKeyFrame KeyTime="0:0:0.3"
Value="0" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
Storyboard.TargetName="rectangle2">
<EasingDoubleKeyFrame KeyTime="0"
Value="0" />
<EasingDoubleKeyFrame KeyTime="0:0:0.3"
Value="1" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)"
Storyboard.TargetName="GridMenu">
<EasingDoubleKeyFrame KeyTime="0"
Value="200" />
<EasingDoubleKeyFrame KeyTime="0:0:0.3"
Value="40" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Opacity)"
Storyboard.TargetName="Title">
<EasingDoubleKeyFrame KeyTime="0"
Value="1" />
<EasingDoubleKeyFrame KeyTime="0:0:0.3"
Value="0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<Grid Name="GridMenu"
Width="40"
HorizontalAlignment="Left"
Background="LightGray">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Height="40"
Background="White"
VerticalAlignment="Top">
<TextBlock Text="Tabs"
Name="Title"
Opacity="0"
FontWeight="Bold"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="Roboto Bk"
FontSize="17" />
</Grid>
<ToggleButton x:Name="ButtonMenu"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Grid.Column="1"
IsChecked="{Binding Expanded}"
BorderBrush="{x:Null}"
Width="40"
Height="40"
Click="ButtonMenu_Click">
<Grid HorizontalAlignment="Center"
VerticalAlignment="Center">
<Rectangle x:Name="rectangle"
Width="30"
Height="5"
Margin="0 0 0 0"
Fill="#FF515151"
HorizontalAlignment="Center"
VerticalAlignment="Top"
RenderTransformOrigin="0.1,1.5">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform />
<SkewTransform />
<RotateTransform />
<TranslateTransform />
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="rectangle1"
Width="30"
Height="5"
Margin="0 20 0 0 "
Fill="#FF515151"
HorizontalAlignment="Center"
VerticalAlignment="Top"
RenderTransformOrigin="0.1,-0.6">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform />
<SkewTransform />
<RotateTransform />
<TranslateTransform />
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="rectangle2"
Width="30"
Height="5"
Margin="0 10 0 0"
Fill="#FF515151"
HorizontalAlignment="Center"
VerticalAlignment="Top" />
</Grid>
</ToggleButton>
<ListView ItemsSource="{Binding Tabs}"
Grid.Row="1"
Background="Transparent"
DisplayMemberPath="ControlName" />
</Grid>
但我只能使用点击事件来制作动画
bool stateClosed = true;
private void ButtonMenu_Click(object sender, RoutedEventArgs e)
{
if (stateClosed)
{
Storyboard sb = FindResource("OpenMenu") as Storyboard;
sb.Begin();
}
else
{
Storyboard sb = FindResource("CloseMenu") as Storyboard;
sb.Begin();
}
stateClosed = !stateClosed;
}
有什么方法可以绑定到 ViewModel 中的任何属性或使用触发器来实现这一点?
解决方案
如果您想用 XAML 代码替换您的代码,您可以使用 RoutedEvents 扩展您的 ToggelBButton。请看下面的代码:
<ToggleButton x:Name="ButtonMenu"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Grid.Column="1"
IsChecked="{Binding Expanded}"
BorderBrush="{x:Null}"
Width="40"
Height="40">
<ToggleButton.Triggers>
<EventTrigger RoutedEvent="ToggleButton.Checked" SourceName="ButtonMenu">
<BeginStoryboard Storyboard="{Binding Source={StaticResource OpenMenu}}"/>
</EventTrigger>
<EventTrigger RoutedEvent="ToggleButton.Unchecked" SourceName="ButtonMenu">
<BeginStoryboard Storyboard="{Binding Source={StaticResource CloseMenu}}"/>
</EventTrigger>
</ToggleButton.Triggers>
...
</ToggleButton>
推荐阅读
- javascript - 使用 macOS High Sierra 安装 React Native Expo 时出错
- google-apps-script - Using folder IDs to delete folders in google drive
- javascript - 如何使用 JavaScript 将 geoJson 文件读入 Mapbox 并利用不透明度?
- php - codeigniter 4 过滤器有问题
- firebase - 跨多个项目访问 Firebase 数据
- javascript - 单击管理面板侧菜单刷新页面而不是展开菜单项
- android - 滚动 NestedScrollView 的内容后如何显示工具栏
- c - 使用递归函数以相反的顺序访问指针指向的数组 - C
- testing - 在前端的每次测试之后清理数据库以进行测试是个好主意吗?
- python - 从 Colab 中的 Google 电子表格获取工作表名称