view raw
Remake Remake - 6 months ago 108
C# Question

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 e.g. in XAML:

<Button x:Name="MyButton" ...>