There is a ComboBox, in it the dimensions of the matrix are from 2 to 10. How to realize that when choosing the size (from СomboBox) a matrix is ​​formed with the dimension equal to the selected value from the ComboBox, and columns and rows of the required size that could be filled out are automatically displayed. WPF is studying for a couple of days and could only make a drop-down list.

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Canvas> <ComboBox Margin="10 10 0 0" ItemsSource="{Binding Count}" SelectedIndex="0" MinWidth="40" HorizontalAlignment="Center" VerticalAlignment="Center"> </ComboBox> </Canvas> </Window> public partial class MainWindow : Window { public int[] Count { get; private set; } private const int Min = 2; private const int Max = 10; public MainWindow() { InitializeComponent(); Count = Enumerable.Range(Min, Max - Min + 1).ToArray(); DataContext = this; } } 
  • Break the task into simple subtasks. 1) How to know that the value in the combo box has changed? 2) According to this value, how to build a matrix with this size? 3) According to this matrix, how to display it. - VladD

1 answer 1

You need to calculate the matrix on the change of the selected item from the ComboBox.

XAML

 <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition /> </Grid.RowDefinitions> <ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" /> <ContentControl x:Name="Matrix" Grid.Row="1" /> </Grid> 

CODE

 public class Content { const int Min = 2; const int Max = 10; public Content(ContentControl contentControl) { this.contentControl = contentControl; items = Enumerable.Range(Min, Max - Min + 1).ToArray(); } readonly ContentControl contentControl; readonly int[] items; public int[] Items { get { return items; } } int selectedItem; public int SelectedItem { get { return selectedItem; } set { selectedItem = value; OnSelectedItemChanged(); } } void OnSelectedItemChanged() { PrintMatrix(); } void PrintMatrix() { var grid = new Grid(); for(int i = 0; i < selectedItem; i++) { grid.ColumnDefinitions.Add(new ColumnDefinition()); grid.RowDefinitions.Add(new RowDefinition()); } for(int i = 0; i < selectedItem; i++) { for(int j = 0; j < selectedItem; j++) { var textBox = new TextBox(); textBox.BorderBrush = Brushes.Gray; textBox.BorderThickness = new Thickness(1); Grid.SetRow(textBox, i); Grid.SetColumn(textBox, j); textBox.Text = i + "," + j; grid.Children.Add(textBox); } } contentControl.Content = grid; } } public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); //Matrix - контрол для вывода матрицы DataContext = new Content(Matrix); } }