66.两组RadioButton的使用 C#例子 WPF例子

通过两组按钮,实现一个简单的乘法运算。RadioButton一组三个按钮一次只能点击一个。

通过对名称进行Switch实现不同的赋值

完整代码:

cs 复制代码
using System.Text;
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 两组按钮2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private double firstnum = 0;
        private double secondnum = 0;

        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if (rb != null)
            {
                if (rb.GroupName == "FirstGroup")
                {
                    switch (rb.Content.ToString())
                    {
                        case "接口1": firstnum = 12.5; break;
                        case "接口2": firstnum = 13; break;
                        case "接口3": firstnum = 19; break;
                    }
                }
                else if (rb.GroupName == "SecondGroup")
                {
                    switch (rb.Content.ToString())
                    {
                        case "信号1": secondnum = 1; break;
                        case "信号2": secondnum = 2; break;
                        case "信号3": secondnum = 10; break;
                    }
                }
                UpdateResult();
            }
        }

        private void UpdateResult()
        {
            TextBlock1.Text = (firstnum * secondnum).ToString();
        }

    }
}
XML 复制代码
<Window x:Class="两组按钮2.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:两组按钮2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid Margin="100 100 200 100">

        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="80"/>
        </Grid.ColumnDefinitions>

        <RadioButton GroupName="FirstGroup" Click="RadioButton_Click" Content="接口1" Grid.Column="0" Grid.Row="0"/>
        <RadioButton GroupName="FirstGroup" Click="RadioButton_Click" Content="接口2" Grid.Column="1" Grid.Row="0"/>
        <RadioButton GroupName="FirstGroup" Click="RadioButton_Click" Content="接口3" Grid.Column="2" Grid.Row="0"/>
        <RadioButton GroupName="SecondGroup" Click="RadioButton_Click" Content="信号1" Grid.Column="0" Grid.Row="1"/>
        <RadioButton GroupName="SecondGroup" Click="RadioButton_Click" Content="信号2" Grid.Column="1" Grid.Row="1"/>
        <RadioButton GroupName="SecondGroup" Click="RadioButton_Click" Content="信号3" Grid.Column="2" Grid.Row="1"/>
        <TextBlock x:Name="TextBlock1" Grid.Row="0" Grid.Column="3" Text="输出中"/>

    </Grid>

</Window>
相关推荐
Sylvia-girl1 小时前
Java——抽象类
java·开发语言
Yana.nice3 小时前
Bash函数详解
开发语言·chrome·bash
江沉晚呤时4 小时前
在 C# 中调用 Python 脚本:实现跨语言功能集成
python·microsoft·c#·.net·.netcore·.net core
Oberon5 小时前
Avalonia硬配.NET Framework 4.8
c#·.net·avalonia·.net framework
tomorrow.hello5 小时前
Java并发测试工具
java·开发语言·测试工具
晓13136 小时前
JavaScript加强篇——第四章 日期对象与DOM节点(基础)
开发语言·前端·javascript
老胖闲聊6 小时前
Python I/O 库【输入输出】全面详解
开发语言·python
code bean6 小时前
【WPF实战】MVVM中如何从数据模型反查自定义控件实例(ImageView + Halcon)
wpf
lph19726 小时前
ValueConverter转换器WPF
wpf
Sally璐璐6 小时前
Memcache核心技术解析与实战应用
运维·wpf·memcached