Transform throws cast error

RogerSchlueter-7899 1,321 Reputation points
2021-07-24T13:33:06.567+00:00

I have a UserControl that includes code that allows it to be moved on a map. This functionality has been working for months but - all of a sudden - it throws a run-time error. Here is the code:

 Private Sub DotMouseLeftButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs) Handles Me.MouseLeftButtonUp
            IsDragging = False
            Dim DraggingDot As Dot = DirectCast(sender, Dot)
            Dim Translate As TranslateTransform = DirectCast(DraggingDot.RenderTransform, TranslateTransform)
            PreviousX = Translate.X
            PreviousY = Translate.Y
            DraggingDot.ReleaseMouseCapture()
        End Sub

The line #4 throws this error:

System.InvalidCastException
HResult=0x80004002
Message=Unable to cast object of type 'System.Windows.Media.MatrixTransform' to type 'System.Windows.Media.TranslateTransform'.

So what's changed, you ask. Last night Microsoft upgraded Windows 10 to the latest release. I really don't like the new theme; but I digress. Nothing else (that I know of) has changed. Could that be the source of the problem? If so, should I report this to Microsoft?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,727 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 117.4K Reputation points
    2021-07-24T13:53:03.633+00:00

    Probably you should not expect that TranslateTransform is the only possible value for RenderTransform. It depends on previous operations.

    Maybe you can use something like this: PreviousX = DraggingDot.RenderTransform.Value.OffsetX.


  2. RogerSchlueter-7899 1,321 Reputation points
    2021-07-24T22:34:46.807+00:00

    Last Comment: I replaced the XAML and vb files for the UseerControl from a backuip. That fixed all the problems. I can only conclude that some non-printing garbage got into the file which then caused all the havoc.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.