如何检查wpf中单选按钮的状态 [英] how check status of radio button in wpf

查看:166
本文介绍了如何检查wpf中单选按钮的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有2个单选按钮,我希望它们在WPF中显示相同的按钮设置它们的样式是按钮。

< Window.Resources> 
< Style TargetType =RadioButton>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate>
< Button Background ={TemplateBinding Property = Background}>
< / Button>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>
< /Window.Resources>
< RadioButton x:Name =rdBlueremoved =BlueCanvas.Left =42Canvas.Top =95
Width =25Height =18/>
< RadioButton x:Name =rdGreenremoved =GreenCanvas.Left =80Canvas.Top =95
Width =25Height =18/>



但是我的问题是我无法检查哪个单选按钮被选中。

这个命令没有单词(它总是返回false) :

  string  imagePath =  < span class =code-string>; 
if (rdBlue.IsChecked.Value == true
{
imagePath = DefineClass.IMAGE_FOLDER_NAME + DefineClass.DEVISION_CHAR + DefineClass.IMAGE_BLUE;
}
其他 如果(rdGreen.IsChecked.Value == true
{
imagePath = DefineClass.IMAGE_FOLDER_NAME + DefineClass.DEVISION_CHAR + DefineClass.IMAGE_GREEN;
}





如何检查此单选按钮是否正在检查?

解决方案

您好,



在您的情况下,您可以将ToggleButton样式应用于单选按钮。它为您提供外观上的WPF按钮样式,并提供radiobutton的检查功能。



以下是如何实现这一点,



而不是使用ControlTemplate,你可以将radiobutton的样式属性绑定到ToggleButton,如下所示:



 <   RadioButton    内容  = 选项    style   =  {StaticResource {x:Type ToggleButton}}     /  >  


Hi All,

I have 2 radio buttons and I want them appearance same button in WPF by set Style of them is buttons.

<Window.Resources>
    <Style TargetType="RadioButton">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate>
            <Button Background="{TemplateBinding Property=Background}">
            </Button>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </Window.Resources>
<RadioButton x:Name="rdBlue" removed="Blue" Canvas.Left="42" Canvas.Top="95"
                   Width="25" Height="18"/>
<RadioButton x:Name="rdGreen" removed="Green" Canvas.Left="80" Canvas.Top="95"
                   Width="25" Height="18"/>


But my issue is i can't check which radio button is checked.
this command don't word(it always return false):

string imagePath = "";
                if (rdBlue.IsChecked.Value == true)
                {
                    imagePath = DefineClass.IMAGE_FOLDER_NAME + DefineClass.DEVISION_CHAR + DefineClass.IMAGE_BLUE;
                }
                else if (rdGreen.IsChecked.Value == true)
                {
                    imagePath = DefineClass.IMAGE_FOLDER_NAME + DefineClass.DEVISION_CHAR + DefineClass.IMAGE_GREEN;
                }



how to check this radio button is checking?

解决方案

Hi,

In your case, you can apply ToggleButton Style to your radio buttons. It gives you WPF button style in appearance and gives radiobutton's checking functionality.

Here is how you can implement this,

Instead of using ControlTemplate, you can bind radiobutton's style property to ToggleButton like following,

<RadioButton Content="Option" style="{StaticResource {x:Type ToggleButton}}" />


这篇关于如何检查wpf中单选按钮的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆