首页 > 解决方案 > 如何在 5x5 网格 WPF C# 中跨越两个按钮的图像

问题描述

试图从图像数组中获取图像以跨越我在 5x5 网格中设置的两个按钮。基于随机生成的 2D 网格,图像被放置在按钮网格上的位置。如果数组中的某个位置 = 2,则将放置一个 2 平方长的图像,对于 r = 3 也是如此。

//这是如果按钮(1,1)被按下

 private void _1_1_Click(object sender, RoutedEventArgs e)
    {
        if (radar.IsChecked == true)
        {
            if (gridarray[0,0] == 1)
            {
                Random rnd = new Random();
                int r = rnd.Next(0,3);
                _1_1.Content = images[r];
                streak.Content = streakn++;
            }
            if (gridarray[0,0] == 2 || gridarray[0,1] == 2)
            {
                Random rnd = new Random();
                int r = rnd.Next(3, 5);
                _1_1.Content = images[r];
                streak.Content = streakn++;
            }
            if (gridarray[0,0] == 3 || gridarray[0,1] == 3 || gridarray[0,2] == 3)
            {
                Random rnd = new Random();
                int r = rnd.Next(5, 7);
                _1_1.Content = images[r];
                streak.Content = streakn++;
            }
        }

// 这里是定义图像数组的地方

    private void defineImages()
    {
        images[0].Source = new BitmapImage(new Uri("Resources/small1.png", UriKind.Relative));
        images[1].Source = new BitmapImage(new Uri("Resources/small2.jpg", UriKind.Relative));
        images[2].Source = new BitmapImage(new Uri("Resources/small3.png", UriKind.Relative));
        images[3].Source = new BitmapImage(new Uri("Resources/medium-1.jpg", UriKind.Relative));
        images[4].Source = new BitmapImage(new Uri("Resources/medium2.jpg", UriKind.Relative));
        images[5].Source = new BitmapImage(new Uri("Resources/large1.png", UriKind.Relative));
        images[6].Source = new BitmapImage(new Uri("Resources/large2.jpg", UriKind.Relative));

    }

// 这是一些相关的 xaml

   <TextBlock Grid.Row="0" Grid.Column="0" Margin="5" FontSize="15">Fish Caught:</TextBlock>
    <TextBlock Grid.Row="1" Grid.Column="0" Margin="5" FontSize="15">Guess Streak:</TextBlock>
    <TextBlock Grid.Row="2" Grid.Column="0" Margin="5" FontSize="15">Fish in Water:</TextBlock>
    <Label Grid.Row="0" Grid.Column="1" Margin="5" FontSize="20" x:Name="caught"></Label>
    <Label Grid.Row="1" Grid.Column="1" Margin="5" FontSize="20" x:Name="streak"></Label>
    <Label Grid.Row="2" Grid.Column="1" Margin="5" FontSize="20" x:Name="fishinwater"></Label>
    <Label Grid.Row="0" Grid.Column="2" Content="Timer:" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="25"/>
    <Label Grid.Column="3" Grid.ColumnSpan="2" Grid.Row="0" Content="Tool Select:" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="25"/>
    <RadioButton x:Name="rod" Grid.Row="1" Grid.Column="3" Content="Rod" Grid.RowSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30"/>
    <RadioButton x:Name="radar" Grid.Row="1" Grid.Column="4" Content="Radar" Grid.RowSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30"/>
    <Button x:Name="_1_1" Grid.Row="3" Grid.Column="0" Background="White" Click="_1_1_Click">
        <Image x:Name="_1"/>
    </Button>
    <Button x:Name="_1_2" Grid.Row="3" Grid.Column="1" Background="White">
        <Image x:Name="_1_2image"/>
    </Button>
    <Button x:Name="_1_3" Grid.Row="3" Grid.Column="2" Background="White">
        <Image x:Name="_1_3image"/>
    </Button>
    <Button x:Name="_1_4" Grid.Row="3" Grid.Column="3" Background="White">
        <Image x:Name="_1_4image"/>
    </Button>
    <Button x:Name="_1_5" Grid.Row="3" Grid.Column="4" Background="White">
        <Image x:Name="_1_5image"/>
    </Button>
    <Button x:Name="_2_1" Grid.Row="4" Grid.Column="0" Background="White">
        <Image x:Name="_2_1image"/>
    </Button>
    <Button x:Name="_2_2" Grid.Row="4" Grid.Column="1" Background="White">
        <Image x:Name="_2_2image"/>
    </Button>
    <Button x:Name="_2_3" Grid.Row="4" Grid.Column="2" Background="White">
        <Image x:Name="_2_3image"/>
    </Button>
    <Button x:Name="_2_4" Grid.Row="4" Grid.Column="3" Background="White">
        <Image x:Name="_2_4image"/>
    </Button>
    <Button x:Name="_2_5" Grid.Row="4" Grid.Column="4" Background="White">
        <Image x:Name="_2_5image"/>
    </Button>
    <Button x:Name="_3_1" Grid.Row="5" Grid.Column="0" Background="White">
        <Image x:Name="_3_1image"/>
    </Button>
    <Button x:Name="_3_2" Grid.Row="5" Grid.Column="1" Background="White">
        <Image x:Name="_3_2image"/>
    </Button>
    <Button x:Name="_3_3" Grid.Row="5" Grid.Column="2" Background="White">
        <Image x:Name="_3_3image"/>
    </Button>
    <Button x:Name="_3_4" Grid.Row="5" Grid.Column="3" Background="White">
        <Image x:Name="_3_4image"/>
    </Button>
    <Button x:Name="_3_5" Grid.Row="5" Grid.Column="4" Background="White">
        <Image x:Name="_3_5image"/>
    </Button>
    <Button x:Name="_4_1" Grid.Row="6" Grid.Column="0" Background="White">
        <Image x:Name="_4_1image"/>
    </Button>
    <Button x:Name="_4_2" Grid.Row="6" Grid.Column="1" Background="White">
        <Image x:Name="_4_2image"/>
    </Button>
    <Button x:Name="_4_3" Grid.Row="6" Grid.Column="2" Background="White">
        <Image x:Name="_4_3image"/>
    </Button>
    <Button x:Name="_4_4" Grid.Row="6" Grid.Column="3" Background="White">
        <Image x:Name="_4_4image"/>
    </Button>
    <Button x:Name="_4_5" Grid.Row="6" Grid.Column="4" Background="White">
        <Image x:Name="_4_5image"/>
    </Button>
    <Button x:Name="_5_1" Grid.Row="7" Grid.Column="0" Background="White">
        <Image x:Name="_5_1image"/>
    </Button>
    <Button x:Name="_5_2" Grid.Row="7" Grid.Column="1" Background="White">
        <Image x:Name="_5_2image"/>
    </Button>
    <Button x:Name="_5_3" Grid.Row="7" Grid.Column="2" Background="White">
        <Image x:Name="_5_3image"/>
    </Button>
    <Button x:Name="_5_4" Grid.Row="7" Grid.Column="3" Background="White">
        <Image x:Name="_5_4image"/>
    </Button>
    <Button x:Name="_5_5" Grid.Row="7" Grid.Column="4" Background="White">
        <Image x:Name="_5_5image"/>
    </Button>
    <Label x:Name="timer"  FontSize="30" Grid.RowSpan="2" Grid.Row="1" Grid.Column="2" Margin="5" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="0"/>

忽略他们不使用的图像标签,图像直接进入按钮。

标签: c#visual-studioxaml

解决方案


推荐阅读