WPF RotateTransform DataTrigger
Asked Answered
E

1

8

Is it possible to use a DataTrigger to set the angle of a RotateTransform in WPF? If so, how?

Estrange answered 3/5, 2012 at 18:10 Comment(0)
S
16

Sure, something like this should work

<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>

Alternatively, if you already have a LayoutTransform set, you could use EnterActions and ExitActions of the DataTrigger

<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>
Skye answered 3/5, 2012 at 18:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.