代码之家  ›  专栏  ›  技术社区  ›  TheSean

如何在按钮模板触发器中使用内容大小?

wpf
  •  0
  • TheSean  · 技术社区  · 15 年前

    我正在为一个按钮创建一个ControlTemplate。我想动画的大小,使其增长,以适应内容时,鼠标悬停在它。但是,我不确定如何获得触发器部分的内容大小。这是我的东西。

    <ControlTemplate x:Key="buttonTemplate" TargetType="Button">
    <Grid Name="grid" Height="12" Width="12" Opacity="0.4">
    <ContentPresenter Name="content" ... />
    </Grid>
    
    <ControlTemplate.Triggers>
        <EventTrigger RoutedEvent="MouseEnter">
            <BeginStoryboard>
                <Storyboard>
            <DoubleAnimation
                Storyboard.TargetName="grid"
                Storyboard.TargetProperty="Width"
                To="40"                         
                Duration="0:0:0.4"/>    
            <DoubleAnimation
                Storyboard.TargetName="grid"
                    Storyboard.TargetProperty="Height"
                To="20"                         
                Duration="0:0:0.4"/>    
            </Storyboard>
        </BeginStoryboard>
        </EventTrigger>
    
        <EventTrigger RoutedEvent="MouseLeave">
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation
                Storyboard.TargetName="grid"
                Storyboard.TargetProperty="Width"
                Duration="0:0:0.2"/>    
            <DoubleAnimation
                Storyboard.TargetName="grid"
                Storyboard.TargetProperty="Height"
                Duration="0:0:0.2"/>                        
            </Storyboard>
        </BeginStoryboard>
        </EventTrigger>
    
    </ControlTemplate.Triggers>
    </ControlTemplate>
    

    TemplateBinding To="40" 但这会导致异常。如何在触发器中使用动态值?

    1 回复  |  直到 15 年前
        1
  •  0
  •   John Bowen    15 年前

    与直接设置布局大小的动画不同,您可以将ScaleTransform应用于栅格并设置其ScaleX和ScaleY值的动画。这不仅可以自动调整大小,而且还可以提供更好的性能,特别是当您将其作为RenderTransform而不是LayoutTransform应用时。如果在动画过程中对布局有其他更改,则可以继续使用LayoutTransform,但由于需要不断重新计算布局度量和排列,因此速度会变慢。