Figuratively speaking, there is an image of a tank that can move in any direction with the help of the arrow buttons on the keyboard + the picture of the tank does not crawl over the edges (it stops at the edge), but at the same time I need the picture to turn not so that it turns out that I'm going upstairs sideways.

below the code as it goes and stops in front of the window (not perfect but in my version works as it should)

private void KeyDowdDo(object sender, KeyEventArgs e) { if (e.Key == Key.Left) if (image.Margin.Left > 10) image.Margin = new Thickness(image.Margin.Left - 15,image.Margin.Top, 0, 0); else image.Margin = new Thickness(image.Margin.Left, image.Margin.Top, 0, 0); if (e.Key == Key.Right) if (image.Margin.Left < 1330) image.Margin = new Thickness(image.Margin.Left + 15, image.Margin.Top, 0, 0); else image.Margin = new Thickness(image.Margin.Left, image.Margin.Top, 0, 0); if (e.Key == Key.Up) if (image.Margin.Top > 10) image.Margin = new Thickness(image.Margin.Left, image.Margin.Top - 15, 0, 0); else image.Margin = new Thickness(image.Margin.Left, image.Margin.Top, 0, 0); if (e.Key == Key.Down) if (image.Margin.Top < 760) image.Margin = new Thickness(image.Margin.Left, image.Margin.Top + 15, 0, 0); else image.Margin = new Thickness(image.Margin.Left, image.Margin.Top, 0, 0); } 

    1 answer 1

     <Image Source="ttt.jpg" RenderTransformOrigin="0.5,0.5"> <Image.RenderTransform> <TransformGroup> <RotateTransform Angle="90"/> </TransformGroup> </Image.RenderTransform> </Image> 

    Similarly in the code

      System.Windows.Media.TransformGroup tg; System.Windows.Media.RotateTransform rt; tg = image.RenderTransform as System.Windows.Media.TransformGroup; if (tg == null) { tg = new System.Windows.Media.TransformGroup(); image.RenderTransform = tg; } rt = tg.Children.FirstOrDefault(x => x is System.Windows.Media.RotateTransform) as System.Windows.Media.RotateTransform; if (rt == null) { rt = new System.Windows.Media.RotateTransform(); tg.Children.Add(rt); } rt.Angle = 90; 

    can be simplified by remembering System.Windows.Media.RotateTransform rt;

      System.Windows.Media.RotateTransform rt = new System.Windows.Media.RotateTransform(); System.Windows.Media.TransformGroup tg = new System.Windows.Media.TransformGroup(); tg.Children.Add(rt); image.RenderTransform = tg; ..... rt.Angle = 90; 

    and do not look for it more in image.RenderTransform

    don't forget to set RenderTransformOrigin="0.5,0.5" in Image

    • Well, you show the man how in the code, right away, he didn’t ask for XAML - ixSci
    • The answer is added. I think the man himself could have guessed this. Still in sight. - Dmitry Chistik
    • Is a fashionable example from C # code? - Ilya Vityuk
    • Is it possible to use an example from the C # code ? Ilya Vityuk
    • I only understand it and for now (what did you expect from me, 14 year old teenager) - Ilya Vityuk