编辑:我试过
Background.SetValue(Grid.RowProperty, 1)
和
Border.Background.SetValue(Grid.RowProperty, 1)
但出现错误提示“无法在对象 '#FFFFFFFFF' 上设置属性,因为它处于只读状态。”
我正在尝试在 WPF 中制作一个随机迷宫生成器。到目前为止,我已经制作了一个起始页和一个“开始”按钮,当您单击开始按钮时,它会引导您进入迷宫,我希望它开始自行生成。
https://i.imgur.com/gPu0rOA.png
我看过教程等,它应该如何设置,以及它背后的整个理论。
现在我有点被一个问题困住了。我希望红色方块移动到右侧或下方的字段,然后为前一个字段着色另一种颜色,以便红色方块是生成迷宫的单元格,而前面的则是迷宫本身。但我似乎根本无法获得更改编程代码中红色方块位置的许可,因为它是只读的。但我需要那个因为我想使用循环等。
<Grid>
<Rectangle Fill="Black" HorizontalAlignment="Left" Height="356" Margin="10,10,0,0" Stroke="Black" VerticalAlignment="Top" Width="772"/>
<Button x:Name="Button1" Content="Start" HorizontalAlignment="Left" Margin="357,380,0,0" VerticalAlignment="Top" Width="74" Click="Button1_Click_1"/>
<TextBlock x:Name="Title1" HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" Height="70" Width="654" Margin="70,71,68,0" Foreground="White" FontSize="60" FontFamily="Impact" FrameworkElement.FlowDirection="LeftToRight" TextAlignment="Center">
<TextBlock.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="Title1" Storyboard.TargetProperty="(FrameworkElement.Height)" To="0.0" Duration="00:00:02" DecelerationRatio="0" AutoReverse="True" BeginTime="00:00:02" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers><Run Text="Random Maze Generator">
<Run.Background>
<ImageBrush/>
</Run.Background>
</Run></TextBlock>
<TextBlock x:Name="Title2" HorizontalAlignment="Left" Margin="246,254,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="54" Width="298" Foreground="White" FontSize="30" Text="Press Start to continue"/>
<Grid Name="GridLines" ShowGridLines="False" Width="772" Height="356" Margin="10,10,10,53">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border Name="Cell" Grid.Row="0" Grid.Column="0" Background="Red" Opacity="0" Visibility="Visible"/>
<StackPanel Name="Stack" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left"></StackPanel>
</Grid>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
double columns = 16;
int rows = 8;
double square_width;
double square_height;
double square_area;
int current_cell;
int visited_cell;
private void Button1_Click_1(object sender, RoutedEventArgs e) //After the Start button is pressed
{
Title1.Text = String.Empty; //Hide text for Title1
Title2.Text = String.Empty; //Hide text for Title2
GridLines.ShowGridLines = true; //Changes the value for GridLines from false to true
Cell.Opacity = 100; //Changes the cell opacity from 0% to 100%
}
}
}
红色方块位于 Xaml 中的 Border.Grid.Row="0" 和 Border.Grid.Column="0" 中,但我不知道如何访问。很长一段时间我都认为我不得不处理的是 Grid.RowProperty 或 Grid.ColumnProperty ,但它们是只读的。
我对编程很陌生,对 WPF 真的很陌生,所以这可能是一个愚蠢的问题,而不是试图触发任何人。
感谢您的时间。