本文介绍了在 WPF 上绘制武士数独网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的 C# WPF 项目上绘制武士数独网格.这是武士数独的例子
I want to draw a samurai sudoku grid on my C# WPF project.this is the example of samurai sudoku
对于网格中的每个 TextBox,我想用 txt 文件中的动态值加载它.
for each TextBox within the grid, I want to load it with dynamic value from the txt file.
txt 文件:23987239847239847"(总共 405 个整数)
txt file : "23987239847239847" (in total 405 integers)
我已经有正常的数独网格工作
I already have the normal sudoku grid working
代码:
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="{Binding Dimension}" Columns="{Binding Dimension}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Text="{Binding Value, Mode=TwoWay}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
绘制看起来像武士 suoku 网格的网格的最有效方法是什么?
What are the most efficient ways to draw a grid that looks like a samurai suoku grid?
推荐答案
示例.它并不像问题中的图片那么难.不过你补充一下也不成问题.
Example for demonstration.It is not as difficult as in the picture in the question.But it will not be a problem for you to supplement it.
public class SudokuCell
{
public Thickness Border { get; }
public int Value { get; set; }
public int Row { get; }
public int Column { get; }
public SudokuCell(int row, int column, Thickness border)
{
Row = row;
Column = column;
Border = border;
}
public SudokuCell(int row, int column, Thickness border, int value)
: this(row, column, border)
{
Value = value;
}
}
public class SudokuViewModel
{
public ObservableCollection<SudokuCell> Cells { get; }
= new ObservableCollection<SudokuCell>();
public IEnumerable<int> ValidValues { get; } = Enumerable.Range(1, 9);
private readonly SudokuCell[,] cellsArray;
private static readonly Random random = new Random();
public SudokuViewModel()
{
cellsArray = new SudokuCell[9, 9];
for (int row = 0; row < 9; row++)
{
for (int column = 0; column < 9; column++)
{
if ((row / 3 + column / 3) % 2 == 1)
continue;
double left = 0.5;
if (column % 3 == 0)
left = 3;
double top = 0.5;
if (row % 3 == 0)
top = 3;
double right = 0.5;
if (column % 3 == 2)
right = 3;
double bottom = 0.5;
if (row % 3 == 2)
bottom = 3;
int value = 0;
if (random.Next(5) < 2)
value = random.Next(9) + 1;
cellsArray[row, column] = new SudokuCell(
row,
column,
new Thickness(left, top, right, bottom),
value);
}
}
foreach (var cell in cellsArray)
{
Cells.Add(cell);
}
}
}
<Window.Resources>
<local:SudokuViewModel x:Key="viewModel"/>
<ItemsPanelTemplate x:Key="Sudoku.Panel">
<UniformGrid Columns="9" Rows="9"/>
</ItemsPanelTemplate>
<DataTemplate x:Key="Sudoku.CellTemplate" DataType="{x:Type local:SudokuCell}">
<Border BorderBrush="SkyBlue" BorderThickness="{Binding Border}"
Background="{Binding Background, ElementName=comboBox}">
<Border.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding}" Value="{x:Null}">
<Setter Property="Border.Opacity" Value="0"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<ComboBox x:Name="comboBox" ItemsSource="{Binding ValidValues, Source={StaticResource viewModel}}"
SelectedItem="{Binding Value}"
FontSize="20" VerticalAlignment="Center" HorizontalAlignment="Center"
BorderThickness="0"/>
</Border>
</DataTemplate>
</Window.Resources>
<ItemsControl ItemsSource="{Binding Cells, Source={StaticResource viewModel}}"
ItemTemplate="{DynamicResource Sudoku.CellTemplate}"
ItemsPanel="{DynamicResource Sudoku.Panel}"/>
这篇关于在 WPF 上绘制武士数独网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!