一、xaml中注释格式<!-- -->
二、Grid 网格布局
<Window x:Class="WpfApplication2.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"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <ColumnDefinition Width="50" ></ColumnDefinition> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition Height="50"></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <!--设置在grid布局中的相对位置--> <Button Grid.Column="1" Grid.Row="1" Background="Azure"></Button> <!--设置在grid布局中跨越网格--> <Button Grid.Column="2" Grid.Row="1" Background="blue" Grid.RowSpan="2"></Button> </Grid> </Window>
二、以代码方式实现grid布局
namespace WpfApplication2 { /// <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(); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { //生成10×10的按钮布局 //Button btn = new Button(); //btn.Content = i + "," + j; //Grid.SetRow(btn, i); //Grid.SetColumn(btn, j); //gridGame.Children.Add(btn); //生成10×10的图片布局 int imgName = random.Next(1,10); Image img = new Image(); img.Source = new BitmapImage(new Uri("Img/"+imgName+".jpg",UriKind.Relative)); Grid.SetRow(img, i); Grid.SetColumn(img, j); gridGame.Children.Add(img); } } } } }