WPF RotateTransform DataTrigger

 ̄綄美尐妖づ 提交于 2019-12-10 14:57:18

问题


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


回答1:


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>


来源:https://stackoverflow.com/questions/10437024/wpf-rotatetransform-datatrigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!