简体   繁体   中英

WPF cast Button RenderTransform to RotateTransform

I want my button's rotation value. People were suggested like this code

RotateTransform rTransform = MyButton.RenderTransform as RotateTransform;
double angle = rTransform.Angle;

But Compiler Thorw this Exception

An unhandled exception of type 'System.InvalidCastException' occurred in DynamicButtonP.exe.

Additional information: Unable to cast a 'System.Windows.Media.MatrixTransform' the object type 'System.Windows.Media.RotateTransform' format.

why I couldn't casting?

You forgot to initialize the RenderTransform property of the Button. Its default value is Transform.Identity , which is a MatrixTransform.

Initialize the property eg in XAML:

<Button x:Name="MyButton" ...>
    <Button.RenderTransform>
        <RotateTransform/>
    </Button.RenderTransform>
</Button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM