Buttons are generated as follows:

private Button[,] CreateButtons(int quantity) { Form.Rows = quantity; Form.Columns = quantity; Button[,] buttons = new Button[quantity, quantity]; for (int i = 0; i < quantity; i++) { for (int j = 0; j < quantity; j++) { buttons[i, j] = new Button(); buttons[i, j].Width = 100; buttons[i, j].Height = 20; buttons[i, j].Margin = new Thickness(5,80,0,0); buttons[i, j].Click += new RoutedEventHandler(new_button_click); } } return buttons; } 

And there is an event handler for clicking on them:

 void new_button_click(object sender, RoutedEventArgs e) { Button btn = sender as Button; if (btn != null) { var transform = new RotateTransform(90); transform.CenterX = 50; transform.CenterY = 10; btn.RenderTransform = transform; } } 

How to make it so that when you click on one button, all buttons in one line and in one column are turned.

    2 answers 2

    You are acting wrong. Take advantage of MVVM. (If you don’t know what it is, get distracted and read, without it, nowhere.)

    Get a VM servicing one button. Get the RotationAngle property in it. Get another VM that will serve all VM buttons, and contain them (let's call it BoardVM ). When activating a button, send a command to its VM. Let this VM call the BoardVM method informing about the click at this point, and that in turn calls the method on each VM in the same column and row, and let this method increase RotationAngle .

    Now View. Bind the list of buttons to the VM list through the ItemsSource any ItemsControl . Assign the Command each button to the command from its VM. Tie a rotation angle to the RotationAngle property. Everything.

    And don't try to program on WPF like on WinForms. It will be very inconvenient and inefficient, believe me.

    • You can see an example of MVVM, for example, here . - VladD
    • thank! I looked at MVVM, indeed, it will be so much easier and more correct to implement. Could you help me understand some points, please? Link - Saint

    In addition to the comments of @VladD, which I advise you to listen to sooner or later, in fact, you can do the following:

     for (int i = 0; i < quantity; i++) buttons[ColumnNumber, i].RenderTransform = new RotateTransform(90) { CenterX = 50, CenterY = 10 }; } for (int i = 0; i < quantity; i++) buttons[i, RowNumber].RenderTransform = new RotateTransform(90) { CenterX = 50, CenterY = 10 }; } 

    Where ColumnNumber and RowNumber corresponding values ​​for a row or column.

    • I apologize, but how do I add this code to the click event handler? Or rather, how do we pass buttons[ColumnNumber, i] to it? - Saint
    • one
      @Saint, I apologize, unfortunately I do not always have the opportunity to respond in time. I am glad that your problem has already been resolved in another post. - Alex Krass