8

是否可以使用 DataTrigger 在 WPF 中设置 RotateTransform 的角度?如果是这样,怎么做?

4

1 回答 1

15

当然,这样的事情应该可以

<TextBox>
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text}"
                             Value="RotateMe">
                    <Setter Property="LayoutTransform">
                        <Setter.Value>
                            <RotateTransform Angle="45"/>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

或者,如果您已经LayoutTransform有一套,您可以使用EnterActionsand ExitActionsofDataTrigger

<TextBox>
    <TextBox.LayoutTransform>
        <RotateTransform Angle="0"/>
    </TextBox.LayoutTransform>
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text}"
                                Value="RotateMe">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:0"
                                                 To="45"
                                                 Storyboard.TargetProperty="(LayoutTransform).(RotateTransform.Angle)"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                    <DataTrigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:0"
                                                 To="0"
                                                 Storyboard.TargetProperty="(LayoutTransform).(RotateTransform.Angle)"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.ExitActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>
于 2012-05-03T18:26:20.827 回答