基于WPF中的绑定开始情节提要

时间:2018-09-26 20:31:06

标签: wpf xaml mvvm data-binding 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>

但是我只能使用click事件制作动画

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中的任何属性或使用触发器来实现此目的?

1 个答案:

答案 0 :(得分:1)

如果您想使用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>