【问题标题】:WPF Radio ButtonsWPF 单选按钮
【发布时间】:2021-08-30 12:19:44
【问题描述】:
我正在 Visual Studio 上创建单选按钮,但我遇到了绑定问题
这是我的按钮:
<RadioButton VericalAlignment="Center" GroupName="group1" Content="name"></RadioButton>
我希望如果按钮被选中,那么 {"someString " + Content Value} 的值将被绑定
到 SomeClass.variable
这样的事情可能吗?
谢谢!
【问题讨论】:
标签:
wpf
xaml
radio-button
【解决方案1】:
这样的事情应该可以工作:
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;
}
}
}