XAML布局:StackPanel、Grid、生成连连看游戏布局


常用于登陆:

<Window x:Class="登录.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="200" Width="400">
    <Grid>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="用户名:"></TextBlock>
            <TextBox Margin="5" Grid.Column="1"></TextBox>
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="密码:" Grid.Row="1"></TextBlock>
            <PasswordBox Margin="5" Grid.Column="1" Grid.Row="1"></PasswordBox>
            <Button Margin="10" Content="登录" Grid.Row="2" Grid.Column="0"></Button>
            <Button Margin="10" Content="取消" Grid.Row="2" Grid.Column="1"></Button>
        </Grid>
    </Grid>
</Window>






动态生成

<Window x:Class="连连看棋盘.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <Grid Name="gridGame">
            
        </Grid>
        <!--<StackPanel Name="sp1" MouseEnter="sp1_MouseEnter"></StackPanel>-->
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 连连看棋盘
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                ColumnDefinition colDef = new ColumnDefinition();
                gridGame.ColumnDefinitions.Add(colDef);

                RowDefinition rowDef = new RowDefinition();
                gridGame.RowDefinitions.Add(rowDef);
            }

            Random random = new Random();
            //int i1= random.Next(0, 99);//生成一个<=0,<99的随机整数
            //int i2= random.Next(3, 9);//生一个<=3,<9的随机整数

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    //Button btn = new Button();
                    //btn.Content = i+","+j;

                    通过代码修改控件的Grid.Row属性
                    //Grid.SetRow(btn, i);
                    //Grid.SetColumn(btn, j);

                    //gridGame.Children.Add(btn);

                    int imgName = random.Next(1, 10);//生成一个>=1,<10的随机数

                    Image img = new Image();
                    //代码给Source属性赋值
                    img.Source = new BitmapImage(new Uri("Images/" + 
                        imgName + ".png", UriKind.Relative));
                    Grid.SetRow(img, i);
                    Grid.SetColumn(img,j);
                    gridGame.Children.Add(img);
                }
            }

            //Button btn = new Button();
            //btn.Content = "hahaha!";

            //sp1.Children.Add(btn);
        }

        private void sp1_MouseEnter(object sender, MouseEventArgs e)
        {
            //Button btn = new Button();//动态创建控件对象
            //btn.Content = "我是新控件";
            //sp1.Children.Add(btn);//把控件加入容器父控件的Children
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值