У меня есть матрица состоящая из TextBlock'ов. Когда я нажимаю на кнопку "Сгенерировать числа" то генерируются все одинаковые числа. Я же хочу сгенерировать все случайные числа. Не пойму, где сделал ошибку.
Код XAML:
<Window x:Class="WPF_Lab_2_part_2.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:WPF_Lab_2_part_2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<Label Content="Ввод матрицы" FontSize="25"/>
</Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" >
<Label Content="Количество строк" Grid.Column="0" HorizontalAlignment="Center"/>
<TextBox Text="4" IsReadOnly="True" Name ="Rows" Margin="5" Width="100" />
</StackPanel>
<StackPanel Grid.Column="1">
<Label Content="Количество столбцов" Grid.Column="0" HorizontalAlignment="Center"/>
<TextBox Text="4" IsReadOnly ="True" Name="Columns" Margin="5" Width="100"/>
</StackPanel>
</Grid >
<Grid Name="MatrixGrid" MinHeight="100" Margin="100 10 100 10">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Name="zero_zero" Grid.Column="0" Grid.Row="0" FontStretch="Expanded"/>
<TextBlock Name="one_zero" Grid.Column="1" Grid.Row="0"/>
<TextBlock Name="two_zero" Grid.Column="2" Grid.Row="0"/>
<TextBlock Name="three_zero" Grid.Column="3" Grid.Row="0"/>
<TextBlock Name="zero_one" Grid.Column="0" Grid.Row="1"/>
<TextBlock Name="one_one" Grid.Column="1" Grid.Row="1"/>
<TextBlock Name="two_one" Grid.Column="2" Grid.Row="1"/>
<TextBlock Name="three_one" Grid.Column="3" Grid.Row="1"/>
<TextBlock Name="zero_two" Grid.Column="0" Grid.Row="2"/>
<TextBlock Name="one_two" Grid.Column="1" Grid.Row="2"/>
<TextBlock Name="two_two" Grid.Column="2" Grid.Row="2"/>
<TextBlock Name="three_two" Grid.Column="3" Grid.Row="2"/>
<TextBlock Name="zero_three" Grid.Column="0" Grid.Row="3"/>
<TextBlock Name="one_three" Grid.Column="1" Grid.Row="3"/>
<TextBlock Name="two_three" Grid.Column="2" Grid.Row="3"/>
<TextBlock Name="three_three" Grid.Column="3" Grid.Row="3"/>
</Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Margin="5 0 5 0" Grid.Column ="0" Content="Сгенерировать числа" Click="Button_Click"/>
<Button Margin="5 0 5 0" Grid.Column ="1" Content="Заменить еденицы нулями"/>
</Grid>
</StackPanel>
</Window>
Код Window.xaml.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPF_Lab_2_part_2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
List<TextBlock> matrx;
public MainWindow()
{
InitializeComponent();
matrx = new List<TextBlock>();
FillMatrix();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
for(int i = 0; i < 4; i++)
{
foreach(TextBlock a in matrx)
{
a.Text = new Random().Next(0, 10).ToString();
}
}
}
private void FillMatrix()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
TextBlock temp = (TextBlock)GetChildren(MatrixGrid, i, j);
matrx.Add(temp);
}
}
}
private static UIElement GetChildren(Grid grid, int row, int column)
{
foreach (UIElement child in grid.Children)
{
if (Grid.GetRow(child) == row
&&
Grid.GetColumn(child) == column)
{
return child;
}
}
return null;
}
}
}