【问题标题】:Random maze generator in WPF [duplicate]WPF中的随机迷宫生成器[重复]
【发布时间】:2019-05-09 00:22:19
【问题描述】:

编辑: 我试过了

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 真的很陌生,所以这可能是一个愚蠢的问题,而不是试图触发任何人。

感谢您的宝贵时间。

【问题讨论】:

  • 附注:如果你想隐藏一个元素(比如隐藏TextBlocks),你最好设置Visibility属性,比如Title1.Visibility = Visibility.Collapsed;
  • 另请注意,不透明度是 0..1 范围内的双精度值,而不是百分比。
  • 是的,很抱歉。我一直在编辑视频和 3D 建模,所以我有点假设它的价值,但它似乎仍然有效。
  • 而不是Border.Background.SetValue(Grid.RowProperty, 1) 使用Cell.SetValue(Grid.RowProperty, 1)
  • 噢耶!!那行得通!非常感谢。我基本上只是做了这个帐户,所以我不知道我是否可以给你任何分数让你回答。如果你知道怎么做,请告诉我。

标签: c# wpf random maze generate


【解决方案1】:

基本上,您的问题是“如何在代码后面设置附加属性?”。 附加属性通常是由赋予其某些角色的控件定义的属性,但通常此属性设置在其他类型的控件上。在您的情况下,网格使用 Column 和 Row 属性来布局子元素。

设置此属性相当简单。您只需像这样的所有关联的 setter 函数:

Grid.SetColumn(Cell, 1);

然而正如 Andy 所说,使用 Grid 并不是最好的选择。

【讨论】:

    【解决方案2】:

    我认为您最好使用画布而不是网格。然后,您可以通过动画 canvas.left 和 canvas.top 轻松地将红色方块从任何位置平滑地动画到另一个位置。

    您可以将控件相互叠加,以便“打开”该块的画布可以位于项目控件的顶部。给它一个 wrappanel 作为 itemspanel,它会环绕你将内容模板化的任何内容,从一行到另一行。 将 cellVM 的集合(每个单元的视图模型)绑定到 itemscontrol 的 itemssource。然后使用 itemtemplate 将每个变成迷宫的一个单元格。这可以像一个矩形一样简单,一种颜色表示闭合,另一种颜色表示打开。 这可以为每个单元格的轮廓设置边框,或者您可以覆盖一个网格图像。 您也可以使用 writeablebitmap 为迷宫背景构建图像,但如果您的要求很简单,itemscontrol 模板相对容易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      相关资源
      最近更新 更多