c# - WPF - 将 Storyboard放入资源字典

标签 c# wpf xaml material-design

我的 MainWindow.xaml 中有这个 Story Board,它基本上使网格隐藏和显示。

<Storyboard x:Key="MenuOpen">
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="GridMenu">
        <EasingDoubleKeyFrame KeyTime="0" Value="50"/>
        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="150"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="MenuClose">
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="GridMenu">
        <EasingDoubleKeyFrame KeyTime="0" Value="150"/>
        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="50"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>

<Window.Triggers>
    <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="btnOpenMenu">
        <BeginStoryboard Storyboard="{StaticResource MenuOpen}"/>
    </EventTrigger>
    <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="btnCloseMenu">
        <BeginStoryboard Storyboard="{StaticResource MenuClose}"/>
    </EventTrigger>
</Window.Triggers>

我想要的是将整个代码放入资源字典中。我已经将它的很大一部分放入了我创建的字典中,但是接下来的部分我不能。我尝试了很多不同的方法,但似乎都不起作用。

<Window.Triggers>
    <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="btnOpenMenu">
        <BeginStoryboard Storyboard="{StaticResource MenuOpen}"/>
    </EventTrigger>
    <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="btnCloseMenu">
        <BeginStoryboard Storyboard="{StaticResource MenuClose}"/>
    </EventTrigger>
</Window.Triggers>

是否可以将触发器放入资源字典中,还是我应该使用其他方式?如果可能的话,正确的方法是什么?

问候。

最佳答案

您可以将 EventTriggers 定义为这样的资源:

<Storyboard x:Key="MenuOpen">
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="GridMenu">
        <EasingDoubleKeyFrame KeyTime="0" Value="50"/>
        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="150"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="MenuClose">
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="GridMenu">
        <EasingDoubleKeyFrame KeyTime="0" Value="150"/>
        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="50"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>

<EventTrigger x:Key="et" RoutedEvent="ButtonBase.Click" SourceName="btnOpenMenu">
    <BeginStoryboard Storyboard="{StaticResource MenuOpen}"/>
</EventTrigger>

<EventTrigger x:Key="et2" RoutedEvent="ButtonBase.Click" SourceName="btnCloseMenu">
    <BeginStoryboard Storyboard="{StaticResource MenuClose}"/>
</EventTrigger>

...并在这样的窗口中引用它们:

<Window.Triggers>
    <StaticResource ResourceKey="et" />
    <StaticResource ResourceKey="et2" />
</Window.Triggers>

关于c# - WPF - 将 Storyboard放入资源字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53153104/

相关文章:

.net - 如何定位绑定(bind)属性的来源

c# - 如何检查我的 C#/WPF 应用程序需要多少 RAM?

c# - 如何将 Selectlist 转换为 Html.SelectListFor()

C# WPF - 拖动并单击

c# - 在 WPF 中使用单独的 ViewModel 将多个选项卡视为单独的 View

WPF 使用 DataTrigger 为 RenderTransform 设置动画

c# - footer IK 光线转换碰到边缘,不知道使用什么旋转

c# - WCF CultureInfo 类型化 DataMember 序列化 CommunicationException

wpf - 在非主 TabItem 内时,绑定(bind)在 DataGrid 列标题中不起作用

c# - 哪个控件可以构建一个十六进制编辑器?