1

我有以下内容Style,其中包含用于扩展 a 的动画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.
4

0 回答 0