在.Resources标记中覆盖ResourceDictionary的StaticResource

时间:2016-03-01 21:28:41

标签: wpf xaml

基本上我有一个Theme.xaml文件,我想在我的MainWindow.xaml中使用它中定义的Storyboard。我可以设法做到这一点,但我想自定义持续时间和开始时间。我想从TextBlock的资源中覆盖这些StaticResource,如下所示:

Theme.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">        
    <Storyboard x:Key="FadeIn">
        <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="{StaticResource StoryboardDuration}" BeginTime="{StaticResource StoryboardBeginTime}">
            <DoubleAnimation.EasingFunction>
                <CubicEase EasingMode="EaseInOut" />
            </DoubleAnimation.EasingFunction>
        </DoubleAnimation>
    </Storyboard>
</ResourceDictionary>

MainWindow.xaml

<Window x:Class="MyApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" ResizeMode="NoResize" Width="512" Height="512">
    <Window.Resources>

        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Theme.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <Grid>
        <TextBlock Text="Should fade in after 3 seconds" Opacity="0">
            <TextBlock.Resources>
                <!-- Define values for storyboard -->
                <sys:TimeSpan x:Key="StoryboardBeginTime">0:0:1</sys:TimeSpan>
                <Duration x:Key="StoryboardDuration">0:0:3</Duration>
            </TextBlock.Resources>
            <TextBlock.Triggers>
                <EventTrigger RoutedEvent="TextBlock.Loaded">
                    <BeginStoryboard Storyboard="{StaticResource FadeIn}" />
                </EventTrigger>
            </TextBlock.Triggers>
        </TextBlock>
    </Grid>
</Window>

上面的代码不会编译,因为没有定义StoryboardBeginTime和StoryboardDuration。但是如果我在Theme.xaml的ResourceDictionary顶部添加以下内容,它会编译,但TextBlock不会覆盖这些值。

<system:TimeSpan x:Key="StoryboardBeginTime">0:0:0</system:TimeSpan>
<Duration x:Key="StoryboardDuration">0:0:1</Duration>

任何人都可以帮助我吗?谢谢。

0 个答案:

没有答案