I need to create swivel handles on the form (by clicking the mouse, the position of the handle changes from vertical to horizontal and back). The handles are located in a square, like a 2-dimensional NxN array. The number N must be customizable. I decided to create an array of rotated buttons.

private Button[,] CreateButtons(int 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 = 50; buttons[i, j].Height = 20; buttons[i, j].VerticalAlignment = System.Windows.VerticalAlignment.Top; buttons[i, j].HorizontalAlignment = System.Windows.HorizontalAlignment.Left; buttons[i, j].Margin = new Thickness(10); } } return buttons; } private void AddToWrapPanel(int quantity, Button[,] buttons) { for (int i = 0; i < quantity; i++) for (int j = 0; j < quantity; j++) { wrapPanel.Children.Add(buttons[i, j]); } } private int GetQuantityButtons() { ComboBoxItem item = (ComboBoxItem)comboBox1.SelectedItem; int count = int.Parse((string)item.Content); return count; } private void СreateButton_Click(object sender, RoutedEventArgs e) { if (wrapPanel.Children.Count > 0) wrapPanel.Children.Clear(); int count = GetQuantityButtons(); Button[,] buttons = CreateButtons(count); AddToWrapPanel(count, buttons); } 

That's what I have. But the problem is on the face - all the buttons are located in a row. How do I get a matrix layout?

    1 answer 1

    Obviously, you need to use more appropriate layout tools for this. For example, replace the WrapPanel with a UniformGrid and bind (manually set) the dimensions in the ComboBox to the Rows and Columns values.

     <ComboBox Name="cmbBox"> <ComboBoxItem>2</ComboBoxItem> <ComboBoxItem>4</ComboBoxItem> <ComboBoxItem>8</ComboBoxItem> </ComboBox> <UniformGrid Columns="{Binding ElementName=cmbBox, Path=SelectedItem.Content}" Rows="{Binding ElementName=cmbBox, Path=SelectedItem.Content}"> </UniformGrid>