今天接着讲控件,说两个也是 最基本的,RadioButton 和 CheckBox 一个单选按钮一个是多选按钮。
非常简单页面的代码如下:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<CheckBox Name="chkApple" Content="苹果" />
<CheckBox Name="chkBanana" Content="香蕉" />
<CheckBox Name="chkMango" Content="芒果" />
<Button Name="btnOK"
Click="btnOK_Click"
Content="确认" />
</StackPanel>
<StackPanel Grid.Row="1">
<RadioButton Click="rbVegetable_Click"
Content="白菜"
GroupName="rdoVegetable" />
<RadioButton Click="rbVegetable_Click"
Content="萝卜"
GroupName="rdoVegetable" />
<RadioButton Click="rbVegetable_Click"
Content="土豆"
GroupName="rdoVegetable" />
</StackPanel>
</Grid>
多选按钮,选中多个后提示选中的哪几个或者一个都没选中,单选按钮是选中哪个提示哪个被选中了。代码如下:
private void btnOK_Click(object sender, RoutedEventArgs e)
{
List<string> items = new List<string>();
if (this.chkApple.IsChecked.Value)
{
items.Add(this.chkApple.Content.ToString());
}
if (this.chkBanana.IsChecked.Value)
{
items.Add(this.chkBanana.Content.ToString());
}
if (this.chkMango.IsChecked.Value)
{
items.Add(this.chkMango.Content.ToString());
}
if (items.Count > 0)
{
string xuanzhong = string.Join(",", items.ToArray());
MessageBox.Show("你选中了" + xuanzhong);
}
else
{
MessageBox.Show("你什么都没有选择");
}
}
private void rbVegetable_Click(object sender, RoutedEventArgs e)
{
RadioButton rb = sender as RadioButton;
MessageBox.Show(rb.Content.ToString());
}
OK 是不是非常简单 效果如图
OK RadioButton 和CheckBox 就说到这里了
代码下载 地址 :http://download.csdn.net/detail/gongkepop/6218629
(写的不好请见谅,有不对请留言告知我,免得误人子弟。)