Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取所选单选按钮的值_C#_Wpf_Visual Studio 2010 - Fatal编程技术网

C# 获取所选单选按钮的值

C# 获取所选单选按钮的值,c#,wpf,visual-studio-2010,C#,Wpf,Visual Studio 2010,我有两个单选按钮,在相同的组名中,用户选择其中一个(默认情况下,未选中任何一个)。当用户选择其中一个时,字符串变量应存储单选按钮的内容。如何实现这一点?首先双击单选按钮以获取其选中的事件 然后在eventhandler中编写以下代码 <RadioButton Content="foo" Checked="radioButton1_Checked" /> <RadioButton Content="bar" Checked="radioButton1_Checked"/>

我有两个单选按钮,在相同的组名中,用户选择其中一个(默认情况下,未选中任何一个)。当用户选择其中一个时,字符串变量应存储单选按钮的内容。如何实现这一点?

首先双击单选按钮以获取其选中的事件 然后在eventhandler中编写以下代码

<RadioButton Content="foo" Checked="radioButton1_Checked" />
<RadioButton Content="bar" Checked="radioButton1_Checked"/>

private void radioButton1_Checked(object sender, RoutedEventArgs e)
{
   variable = (string)(sender as RadioButton).Content;
}

已选中专用无效radioButton1_(对象发送器,路由目标e)
{
变量=(字符串)(发送者作为单选按钮)。内容;
}

我猜您的意思是“单击”而不是双击您可能只能在父容器上使用事件路由和句柄检查。@H.B.您好,想知道下面的代码有什么问题吗?public void单选按钮已选中(object sender,RoutedEventArgs e){object variable=(string)(sender as RadioButton).Content;}我试图添加对象并更改为public而不是private,我这样做是因为我想在按钮单击方法中使用该变量。怎么做?@JackyBoi:引入一个(和/或)并分配给它。可能会有帮助,将
SelectedItem
绑定到某个属性。如果我真的理解,可能会有帮助!!我是新来的,所以有困难的时间让我的头周围简单的答案将是很好的tks