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) { Random ran = new Random(); //定义十行,十列 for (int i = 0; i < 10; i++) { RowDefinition row = new RowDefinition(); gridLLk.RowDefinitions.Add(row); ColumnDefinition column = new ColumnDefinition(); gridLLk.ColumnDefinitions.Add(column); } 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);//改动btn控件的Grid.Row属性为i //Grid.SetColumn(btn, j); //gridLLk.Children.Add(btn); int imgName = ran.Next(1, 10); Image img=new Image(); //UriKind.Relative表示uri是相对uri //BitmapImage(Uri) 使用由提供的 Uri, BitmapImage 初始化类的新实例。 img.Source = new BitmapImage(new Uri("images/"+imgName+".jpg", UriKind.Relative)); Grid.SetRow(img,i); Grid.SetColumn(img,j); gridLLk.Children.Add(img); } } } } }