WPF RadioButton 基本使用演示

效果如下图:

实现:

1.获取选中Item的内容

2.实现分组选择

3. 绑定 Item的图片和文字显示的定制显示

XMAL代码如下:

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp"
        mc:Ignorable="d"
        Title="Options demo" Height="450" Width="400">
    <Grid>
        <StackPanel Margin="10">

            <StackPanel Margin="5">
                <StackPanel Margin="5" >
                    <RadioButton x:Name="r1" Content="Varta" Checked="r_Checked"></RadioButton>
                    <RadioButton x:Name="r2" Content="Panasonic" Checked="r_Checked"></RadioButton>
                    <RadioButton x:Name="r3" Content="Others" Checked="r_Checked"></RadioButton>
                </StackPanel>
                <TextBox x:Name="txtMsg" Margin=" 2"></TextBox>
            </StackPanel>

            <Separator Height="5" Background="Green"/>

            <StackPanel Margin="5" x:Name="sp2">
                <Label>RadioButton 分组</Label>
                <Label FontWeight="Bold">Are you ready?</Label>
                <RadioButton GroupName="ready">Yes</RadioButton>
                <RadioButton GroupName="ready">No</RadioButton>
                <RadioButton GroupName="ready" IsChecked="True">Maybe</RadioButton>

                <Label FontWeight="Bold">Male or female?</Label>
                <RadioButton GroupName="sex">Male</RadioButton>
                <RadioButton GroupName="sex">Female</RadioButton>
                <RadioButton GroupName="sex" IsChecked="True">Not sure</RadioButton>
            </StackPanel>

            <StackPanel Margin="5" >
                <Label FontWeight="Bold">Please Choose one?</Label>
                <RadioButton>
                    <WrapPanel>
                        <Image Source="Images/yes.png" Width="16" Height="16" Margin="0,0,5,0" />
                        <TextBlock Text="Yes" Foreground="Green" />
                    </WrapPanel>
                </RadioButton>
                <RadioButton Margin="0,5">
                    <WrapPanel>
                        <Image Source="Images/no.png" Width="16" Height="16" Margin="0,0,5,0" />
                        <TextBlock Text="No" Foreground="Red" />
                    </WrapPanel>
                </RadioButton>
                <RadioButton IsChecked="True">
                    <WrapPanel>
                        <Image Source="Images/question.png" Width="16" Height="16" Margin="0,0,5,0" />
                        <TextBlock Text="Maybe" Foreground="Gray" />
                    </WrapPanel>
                </RadioButton>
            </StackPanel>

        </StackPanel>
    </Grid>
</Window>
 

后台代码如下:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void r_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton RB = sender as RadioButton;
            if (RB.IsChecked == true)
            {
                txtMsg.Text = RB.Content.ToString();

                #region 选择
                //switch (RB.Name)
                //{
                //    case "r1":
                //        txtMsg.Text = "瓦尔塔";
                //        break;
                //    case "r2":
                //        txtMsg.Text = "松下";
                //        break;
                //    case "r3":
                //        txtMsg.Text = "其它";
                //        break;

                //} 
                #endregion
            }
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysh05

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值