假设有一个网格。分成两行。最上面的还有一个网格,它被 3 除以 3(我正在创建一个标签游戏)。在代码中需要创建 8 个按钮(稍后会添加随机性),每个单元格中除了一个。http://prntscr.com/jb9xh5标记屏幕。和底部的标记代码本身。一般来说,你需要在c#中必要的单元格中创建按钮
<Window x:Class="Game_PYATNASHKI.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:Game_PYATNASHKI"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" x:Name="Pole">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
</Grid>
</Grid>
对我来说,通过代码设置按钮并不是什么好事,这里需要用到绑定工具和MVVM。
最简单的选项(就我而言)是使用 ItemsControl,它附加了一个带有其 VM 的集合,该集合又包含有关按钮的数据。
也就是说,我们的标记会变成这样:
按钮的 VM 将如下所示:
好吧,仍然需要创建一个集合,我们将向其中添加几个按钮:
结果将是这样的: