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>
相关推荐
关关钧1 小时前
【R语言】plyr包和dplyr包
开发语言·r语言
YGGP1 小时前
【GeeRPC】7天用 Go 从零实现 RPC 框架 GeeRPC
开发语言·rpc·golang
Android系统攻城狮1 小时前
Rust语言进阶之标准输出:stdout用法实例(一百零六)
开发语言·后端·rust
火鸟21 小时前
蛋糕商城 Rust 版介绍
开发语言·后端·rust·bootstrap·蛋糕商城·axum·开源例程
沉到海底去吧Go2 小时前
【扫描件PDF】如何批量识别扫描件PDF多个区域内容保存到Excel表格,基于WPF和腾讯OCR的详细解决方案
pdf·wpf·excel·pdf批量个性化拆分·批量按照分拣大小拆分pdf·批量拆分pdf保存到指定位置·pdf区域识别实现方案
军训猫猫头2 小时前
69.弹窗显示复杂的数据框图 C#例子 WPF例子
ui·c#·wpf
^@^lemon tea^@^2 小时前
WPF 进度条(ProgressBar)示例一
wpf·progressbar示例·wpf 进度条示例
zzlyx992 小时前
基于 C# 开源的Netnr.Login组件库,maui开发实现 QQ、微信等多种主流第三方平台的登录授权
开源·c#·移动app
天堂的恶魔9465 小时前
C++封装
java·开发语言·c++