Remake Remake - 1 year ago 292
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?

Answer Source

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" ...>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download