常用于登陆:
<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
}
}
}