基于单选按钮显示控件选择 [英] Displaying controls based on radio button selected

查看:116
本文介绍了基于单选按钮显示控件选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组三个单选按钮。根据选择哪个单选按钮,我想disaply三个控制一个 - 一个文本框,下拉列表或按钮。如何显示的基础上选定的单选按钮的结果控制?

I have a group of three radio buttons. Depending on which radio button is selected, I want to disaply one of three controls - a textbox, a dropdown list, or a button. How do I display controls based on the result of a selected radio button?

推荐答案

您可以将控件绑定到单选按钮的器isChecked财产的知名度,使用 BooleanToVisibilityConverter

You can bind the visibility of the control to the IsChecked property of the RadioButton, using the BooleanToVisibilityConverter :

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
    <BooleanToVisibilityConverter x:Key="convVisibility"/>
  </Page.Resources>
  <Grid>
    <StackPanel Orientation="Vertical">
      <RadioButton Name="radioButton1" GroupName="group1">Control1</RadioButton>
      <RadioButton Name="radioButton2" GroupName="group1">Control2</RadioButton>
      <RadioButton Name="radioButton3" GroupName="group1">Control3</RadioButton>
      <Grid>
        <Button Visibility="{Binding IsChecked, ElementName=radioButton1, Converter={StaticResource convVisibility}}">1. Button</Button>
        <TextBlock Visibility="{Binding IsChecked, ElementName=radioButton2, Converter={StaticResource convVisibility}}">2. TextBlock</TextBlock>
        <TextBox Visibility="{Binding IsChecked, ElementName=radioButton3, Converter={StaticResource convVisibility}}">3. TextBox</TextBox>
      </Grid>
    </StackPanel>
  </Grid>
</Page>

编辑:

这是解决方案的伟大工程,这是很容易实现。反正我有可以prevent控件被隐藏在设计模式?

That solutions works great and it's simple to implement. Is there anyway I can prevent the controls from being hidden in design mode?

我不知道其他设计师(混合为例),但在Visual Studio设计的控制是从来没有隐瞒过...

I don't know about other designers (Blend for instance), but in the Visual Studio designer the controls are never hidden...

总之,你可以实现自己的转换器,它总是在设计模式返回可见。对于一些模糊的原因,BooleanToVisibilityConverter类是密封的,所以你不能从它继承。您可以委托转换为BooleanToVisibilityConverter相反,如果你不希望重写转换逻辑:

Anyway, you could implement your own converter, which would always return Visible in design mode. For some obscure reason the BooleanToVisibilityConverter class is sealed, so you can't inherit from it. You can delegate the conversion to a BooleanToVisibilityConverter instead, if you don't want to rewrite the conversion logic :

public class MyBooleanToVisibilityConverter : IValueConverter
{
    private BooleanToVisibilityConverter _converter = new BooleanToVisibilityConverter();
    private DependencyObject _dummy = new DependencyObject();

    private bool DesignMode
    {
        get
        {
            return DesignerProperties.GetIsInDesignMode(_dummy);
        }
    }

    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (DesignMode)
            return Visibility.Visible;
        else
            return _converter.Convert(value, targetType, parameter, culture);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return _converter.ConvertBack(value, targetType, parameter, culture);
    }

    #endregion
}

这篇关于基于单选按钮显示控件选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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