2021-08-08 WPF控件专题 CheckBox控件详解
【摘要】
1.CheckBox控件介绍
复选框 允许可以选择多个 ContentControl
常用属性 Content Name IsChecked(true false null) IsThreeState...
1.CheckBox控件介绍
复选框 允许可以选择多个 ContentControl
常用属性 Content Name IsChecked(true false null) IsThreeState Tag
动态添加
获取已勾选选项 父容器 —Grid Children —子元素的集合
2.具体案例
<Grid Name="grid1">
<!--中间状态时 IsChecked 空-->
<!--<CheckBox Name="chkSport" Content="体育" IsChecked="True" IsThreeState="True" HorizontalAlignment="Left" Margin="264,117,0,0" VerticalAlignment="Top"/>
<CheckBox Content="唱歌" HorizontalAlignment="Left" Margin="337,120,0,0" VerticalAlignment="Top"/>
<CheckBox Content="跳舞" HorizontalAlignment="Left" Margin="398,120,0,0" VerticalAlignment="Top"/>-->
<Button Content="添加" HorizontalAlignment="Left" Margin="255,169,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<Button Content="获取" Name="btnGet" HorizontalAlignment="Left" Margin="369,169,0,0" VerticalAlignment="Top" Width="75" Click="BtnGet_Click"/>
</Grid>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
private void Button_Click(object sender, RoutedEventArgs e)
{
//MessageBox.Show(chkSport.IsChecked.ToString());
//代码动态添加CheckBox
string[] names = { "体育", "唱歌", "跳舞", "绘画" };
for (int i = 0; i < names.Length; i++)
{
CheckBox chk = new CheckBox();
chk.Content = names[i];
chk.HorizontalAlignment = HorizontalAlignment.Left;
chk.VerticalAlignment = VerticalAlignment.Top;
chk.Margin = new Thickness(40+i*80, 60, 0, 0);
grid1.Children.Add(chk);
}
}
private void BtnGet_Click(object sender, RoutedEventArgs e)
{
//获取窗口中所有勾选的CheckBox的Content
string strContens = "";
foreach (UIElement ele in grid1.Children)
{
if (ele is CheckBox)
{
CheckBox chk = ele as CheckBox;
if (chk.IsChecked == true)
{
if (strContens != "")
strContens += ",";
strContens += chk.Content.ToString();
}
}
}
MessageBox.Show(strContens);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/119518392
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)