C#后台实现在Grid标签中动态新增CheckBox标签(WPF中)
- 页面代码
1 <Grid Margin="45,0,10,0" > 2 <Grid.RowDefinitions> 3 <RowDefinition Height="*"/> 4 <RowDefinition Height="*"/> 5 </Grid.RowDefinitions> 6 <Grid Name="controlReasonGrid"> 7 <Grid.ColumnDefinitions> 8 <ColumnDefinition Width="*"/> 9 <ColumnDefinition Width="*"/> 10 <ColumnDefinition Width="*"/> 11 <ColumnDefinition Width="*"/> 12 <ColumnDefinition Width="*"/> 13 <ColumnDefinition Width="*"/> 14 <ColumnDefinition Width="*"/> 15 <ColumnDefinition Width="*"/> 16 <ColumnDefinition Width="*"/> 17 <ColumnDefinition Width="*"/> 18 <ColumnDefinition Width="*"/> 19 <ColumnDefinition Width="*"/> 20 </Grid.ColumnDefinitions> 21 <CheckBox Margin="10,0" Name="controlReason1" Content="北京" Style="{StaticResource CommonCheckBoxStyle}" /> 22 <CheckBox Grid.Column="1" Name="controlReason2" Content="上海" Style="{StaticResource CommonCheckBoxStyle}" /> 23 <CheckBox Grid.Column="2" Name="controlReason3" Margin="10,0,0,0" Content="深圳" Style="{StaticResource CommonCheckBoxStyle}" /> 24 <CheckBox Grid.Column="3" Name="controlReason4" Content="广州" Style="{StaticResource CommonCheckBoxStyle}"/> 25 26 <TextBox Name="controlReasonTextBox" Grid.Column="4" Style="{StaticResource CommonTextBoxStyle}" Height="26" Margin="29,0,0,0" /> 27 <Button Grid.Column="5" Name="controlReasonButton" Content="新 增" Width="50" Style="{StaticResource CommonButtonStyle}" Margin="0,0,84,0" Click="controlReasonButton_Click"/> 28 </Grid> 29 </Grid>
- 后台代码
1 List<CheckBox> m_AllCheckBoxs2 = new List<CheckBox>();//存放checkBox 2 3 private void controlReasonButton_Click(object sender, RoutedEventArgs e) 4 { 5 6 if (m_AllCheckBoxs2.Count == 9) 7 { 8 controlReasonTextBox.Visibility = Visibility.Collapsed; 9 controlReasonButton.Visibility = Visibility.Collapsed; 10 } 11 if (controlReasonTextBox.Text == "") 12 { 13 MessageBox.Show("值不能为空!"); 14 return; 15 } 16 foreach (var item in m_AllCheckBoxs2) 17 { 18 if (controlReasonTextBox.Text == item.Content.ToString()) 19 { 20 MessageBox.Show("值不能相同!"); 21 return; 22 } 23 } 24 AddCheckBox(""); 25 26 } 27 private void AddCheckBox(string checkValue) 28 { 29 int prev = Convert.ToInt32(m_AllCheckBoxs2.Last().GetValue(Grid.ColumnProperty)) + 1;//获取集合最后一个元素的列 30 if (m_AllCheckBoxs2.Count > 10) return; 31 CheckBox checkBox = new CheckBox 32 { 33 Name = "controlReason" + (prev) + "", 34 Content = checkValue != "" ? checkValue : controlReasonTextBox.Text, 35 IsChecked = checkValue != "" ? true : false, 36 }; 37 foreach (var item in m_AllCheckBoxs2) 38 { 39 if (item.Content.ToString() == checkBox.Content.ToString()) 40 { 41 return; 42 } 43 } 44 checkBox.SetValue(CheckBox.StyleProperty, Application.Current.Resources["CommonCheckBoxStyle"]);//绑定样式 45 checkBox.SetValue(Grid.ColumnProperty, prev); //设置按钮所在Grid控件的列 46 controlReasonGrid.Children.Add(checkBox); 47 48 int curr = Convert.ToInt32(checkBox.GetValue(Grid.ColumnProperty)) + 1;//获取当前复选框所在列 49 controlReasonTextBox.SetValue(Grid.ColumnProperty, curr);//文本框 50 51 int prev2 = Convert.ToInt32(controlReasonTextBox.GetValue(Grid.ColumnProperty)) + 1;//获取文本框所在列 52 controlReasonButton.SetValue(Grid.ColumnProperty, prev2);//新增按钮 53 checkBox.Checked += cb_Checked2;//绑定事件 54 checkBox.Unchecked += cb_Unchecked2;//绑定事件 55 ChangeSelAllChecked(checkBox, m_AllCheckBoxs2); 56 57 m_AllCheckBoxs2.Add(checkBox); 58 }