将DoubleAnimation设置为绑定到外部对象

时间:2014-12-16 09:46:15

标签: wpf xaml storyboard wpf-animation

我有以下Style,其中包含用于展开Border的动画:

<Style TargetType="{x:Type Border}" x:Key="BorderAnimations">
    <Setter Property="FlowDirection" Value="RightToLeft" />
    <Style.Triggers>
        <EventTrigger RoutedEvent="MouseEnter">
            <BeginStoryboard>
                <Storyboard >
                    <DoubleAnimation
                        Storyboard.TargetProperty="Width"
                        BeginTime="0:0:0"
                        From="" To="420" Duration="0:0:0.2" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="MouseLeave">
            <BeginStoryboard>
                <Storyboard >
                    <DoubleAnimation
                        Storyboard.TargetProperty="Width"
                        BeginTime="0:0:0"
                        From="420" To="90" Duration="0:0:0.2" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Style.Triggers>
</Style>

我想将From属性设置为“泛型”按钮宽度。这里我有通用Button style

 <Style TargetType="{x:Type Button}">
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FontSize" Value="80" />
    <Setter Property="HorizontalAlignment" Value="Right"/>
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Background" Value="{DynamicResource MainBlue}" />
    <Setter Property="Height" Value="{Binding 
                ElementName=MainUCPanel, Path=Height, 
                Converter={StaticResource ArithmeticConverter}, 
                ConverterParameter=Int32.Parse(values[0]) / 6}" />
    <Setter Property="Width" Value="{Binding Path=Height, RelativeSource={RelativeSource Self}}" />
    <Setter Property="DataContext" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type view:DesktopUCMain}}, Path=DataContext}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}" BorderBrush="Transparent" BorderThickness="0" CornerRadius="5" x:Name="bd">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                    Margin="5" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Cursor" Value="Hand" />
            <Trigger.ExitActions>
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="#FF1E90FF" Duration="0:0:0.2" />
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.ExitActions>
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="#27ace3" Duration="0:0:0.2" />
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
        </Trigger>
    </Style.Triggers>
</Style>

我试图将其设置为:

From="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}, Path=ActualWidth}"

但它不起作用......

注意:这些样式位于 ResourceDictionary.xaml 上。如何实现动画以获得此宽度?

编辑:引发异常:

Cannot freeze this Storyboard timeline tree for use across threads.

0 个答案:

没有答案