首页 > 解决方案 > WPF 单选按钮

问题描述

我在 Visual Studio 上创建单选按钮,但我遇到了绑定问题

这是我的按钮:

<RadioButton VericalAlignment="Center" GroupName="group1" Content="name"></RadioButton>

我希望如果选择了按钮,则 {"someString" + Content Value} 的值将绑定到 SomeClass.variable

这样的事情可能吗?谢谢!

标签: wpfxamlradio-button

解决方案


像这样的东西应该工作:

XAML:

<Window x:Class="StackOverflowRadioButton.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:StackOverflowRadioButton"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid Margin="81,36,-81,-36">
    <RadioButton Content="RadioButton" HorizontalAlignment="Left" Margin="111,135,0,0" VerticalAlignment="Top"
                 Name="myRadioButton" Checked="myRadioButton_Checked"/>
</Grid>

CS文件:

using System.Windows;

namespace StackOverflowRadioButton
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public string someVariable;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void myRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            someVariable = "someString " + myRadioButton.IsChecked;
        }
    }
}

推荐阅读