前几天在博客园,看到有博友利用Winform做了一个口算案例,于是我想把它移植在WPF程序中。Winform程序:http://www.cnblogs.com/ImYZF/p/3345452.html
WPF中:
个人感觉在WPF中动态创建完控件之后,无法有像Winform中FindName()这样的方法来对控件进行搜寻,因此我采用的方法是在布局控件中动态创建控件后,用for循环遍历布局中的控件,然后利用
布局控件的Children属性进行对控件的定位。
上代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Data; 9 using System.Windows.Documents; 10 using System.Windows.Input; 11 using System.Windows.Media; 12 using System.Windows.Media.Imaging; 13 using System.Windows.Navigation; 14 using System.Windows.Shapes; 15 16 namespace 口算训练 17 { 18 /// <summary> 19 /// Interaction logic for MainWindow.xaml 20 /// </summary> 21 public partial class MainWindow : Window 22 { 23 public MainWindow() 24 { 25 InitializeComponent(); 26 } 27 28 private void btnAdd_Click(object sender, RoutedEventArgs e) 29 { 30 if (txtNum.Text == "") 31 { 32 MessageBox.Show("请输入数目!"); 33 return; 34 } 35 //清空布局控件中的控件 36 grid2.Children.Clear(); 37 int totalNumber = int.Parse(txtNum.Text); 38 Random random = new Random(); 39 TextBox tb = new TextBox(); 40 Label l = new Label(); 41 for (int i = 0; i < totalNumber; i++) 42 { 43 //根据问题的数量设置row的个数 44 grid2.RowDefinitions.Add(new RowDefinition() { Height=new GridLength(30)}); 45 for (int j = 1; j <=3; j++) 46 { 47 //设置文本框的一些属性 48 tb = new TextBox(); 49 tb.Width = 80; 50 tb.Height = 25; 51 tb.Name = "txt" + Convert.ToString(i)+Convert.ToString(j); 52 //附加属性 53 tb.SetValue(Grid.RowProperty, i); 54 tb.SetValue(Grid.ColumnProperty,(j-1)*2); 55 56 if (j <= 2) 57 { 58 //产生随机数,作为加数 59 tb.Text = Convert.ToString(random.Next(10)); 60 tb.IsReadOnly = true; 61 } 62 //添加子控件 63 grid2.Children.Add(tb); 64 l = new Label(); 65 66 //创建Label 67 switch (j) { 68 case 1: l.Width = 30; l.Height = 20; l.Content = "+"; l.SetValue(Grid.RowProperty, i); l.SetValue(Grid.ColumnProperty, j); 69 break; 70 case 2: l.Width = 30; l.Height = 20; l.Content = "="; l.SetValue(Grid.RowProperty, i); l.SetValue(Grid.ColumnProperty, j + 1); 71 break; 72 case 3: l.Width = 50; l.Height = 30; ; l.Name = "labelresult" + Convert.ToString(i); ; l.SetValue(Grid.RowProperty, i); l.SetValue(Grid.ColumnProperty, j + 2); 73 break; 74 75 } 76 grid2.Children.Add(l); 77 78 79 } 80 81 } 82 } 83 84 private void btnResult_Click(object sender, RoutedEventArgs e) 85 { 86 int totalNumber=int.Parse(txtNum.Text); 87 TextBox[] tbs; 88 for (int i = 1; i <=totalNumber; i++) 89 { 90 91 tbs = new TextBox[3]; 92 //获取有关TextBox控件 93 tbs[0] = grid2.Children[6 * (i-1)] as TextBox; 94 tbs[1] = grid2.Children[6 * (i-1) + 2] as TextBox; 95 tbs[2] = grid2.Children[6 * (i-1) + 4] as TextBox; 96 97 Label labelresult = grid2.Children[6 * (i-1) + 5] as Label; 98 //如果未填答案,按错误处理 99 if (tbs[2].Text == "") 100 { 101 labelresult.Content = "X"; 102 continue; 103 } 104 int add = Convert.ToInt32(tbs[0].Text) + Convert.ToInt32(tbs[1].Text); 105 if (add == Convert.ToInt32(tbs[2].Text)) 106 { 107 labelresult.Content = "right!"; 108 } 109 else 110 { 111 labelresult.Content = "X"; 112 } 113 } 114 } 115 } 116 }