Displaying controls based on radio button selected

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?

-------------Problems Reply------------

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>

EDIT :

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

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

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
}

To keep the component visible during design time you can add a FallbackValue of true to the checkbox as:

<RadioButton x:Name="cbxEmail" Content="Email Details" IsEnabled="{Binding IsEmail, FallbackValue=true}" IsChecked="{Binding IsEmail, Mode=OneWay, FallbackValue=true}"
Grid.Column="2"/>

Category:.net Views:0 Time:2009-06-25

Related post

  • How to validate a text box based on radio button selection in ASP.NET MVC 3 and JQuery? 2012-03-26

    I have a radio button with values, "Yes" and "No". If user selects "Yes", the text box slides down. So it has to be validated. If user clicks "No", the textbox slides up and there is no need for validation. I tried to use, ValidationMessageFor(model

  • Display different results based on radio button selection 2011-11-02

    I'm using a piece of JQuery to create the effect described at this website: http://www.onextrapixel.com/2009/10/13/how-to-display-form-fields-based-on-selection-with-or-without-jquery-cookie/ What I need is to be able to display a different div based

  • Best way to swap two .NET controls based on radio buttons 2008-09-25

    I've got a form where I have two radio buttons and two interchangeable controls (made up of a ListView and a handful of buttons). Based on which radio button is selected I want to display the proper control to the user. The way I'm doing this now is

  • How to use jQuery to show/hide divs based on radio button selection? 2010-05-05

    I have some radio buttons and I'd like to have different hidden divs show up based on which radio button is selected. Here's what the HTML looks like: <form name="form1" id="my_form" method="post" action=""> <div><label><input ty

  • jquery toggle div based on radio button selection 2011-07-27

    I have a radio button group on a survey form whose html looks like: <label>Would you like to compete?</label> <input type="radio" name="compete" value="2" id="competenoradio" > <label for="compete_no">No</label> <inpu

  • Enable text box based on radio button selected 2012-04-12

    I have a Radio Button List that lists different business categories. The last option is the Other category. When the user selects the Other category, I want to be able to enable a text box that the user can then enter further information to explain t

  • HTML Form Validation - Conditional based on radio button selection 2014-06-03

    This question already has an answer here: jQuery validate - set conditional rules based on user selection 3 answers I have an HTML form and I'm using the JQuery Validate plugin to require some form fields to be mandatory. I have a radio button field

  • Add hidden input types based on radio button selected 2011-09-08

    Is it possible to conditionally add hidden input fields into a form? eg I have a php form that is adding values to a table and if the appleID = 1 or 2 then I want 1 added to the fruits column of my table and if appleID =3 I want 1 added to the sweets

  • jquery validation based on radio button selection 2011-02-18

    In my form i have two radio buttons(seller and buyer).same form fields for both radio buttons. But the diffrence is while i click buyer radio button some fields will hide i used javascript for hide that. Then i used jquery form validation.Also used j

  • changing form behavior on submit based on radio button selection 2011-08-10

    I'm using Rails 3.1.0.rc5. I want to have a form with a pair of radio buttons, enable and disable, and a field to enter an integer (expire_after_days, the number of days until ticket expiration), with a hidden field for the fixed parameter subdomain_

  • Check Input text if empty based upon radio button selected 2011-08-25

    I have two radiobuttons in a group on my page. Based upon radiobutton selected i want to generate an alert. var d=GetVal(); function GetVal() { var a = null; var f = document.forms[0]; var e = f.elements["radiogroup"]; for (var i=0; i < e.length;

  • jQuery , show and hide divs based on radio button click 2011-05-09

    This question already has an answer here: How to use jQuery to show/hide divs based on radio button selection? 6 answers So I want to be able to dynamically change what divs are show using radio button and jQuery - HTML: <div id="myRadioGroup">

  • format for url to point to radio button selection 2011-01-20

    Possible Duplicate: linking to a radio button selection, asp.net c# i have a page with a textarea and radio buttons. the text area is populated with data based on the radio button selection. i want the radio button selection to appear in the url so t

  • Classic ASP: Dynamic dropdown bases on a radio button selected 2011-05-10

    I'm new to Classic ASP. I want to display the list in dropdown based on one of the radio buttons selected. The database has the column for the Code such as "A" and "O". On the page, I want to display 3 radio buttons as A, O, and All. A = Apples, O =

  • how to change the select list box according to the radio button selects 2012-01-05

    I have two drop down lists , list1, list 2. I want it to change according to the radio button selected by the user. If the user selects radio button1, lists1 should be displayed. If the user selects radio button2, lists2 should be displayed. How can

  • How to get radio button selection with JQuery? 2009-09-04

    I have MVC html control for radiobutton like: <%= Html.RadioButton("Choice", false, new { onclick = "Accept()" })%><label for="Choice">Yes</label> <%= Html.RadioButton("Choice", false, new { onclick = "Deny()" })%><label fo

  • VB.NET default radio button selected inside a group box 2010-01-08

    I have a WinForms application (VS 2008, .NET 3.5) that has a form with two different group boxes, and inside of each group box are different sets of radio buttons. When I run the application, the first group box automatically has the first radio butt

  • Android: How to get text of dynamically created radio button selected by the user? 2010-06-18

    How can i retrieve the text of a dynamically created radio button selected by the user? Here's my code: RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1); // layout params to use when adding each radio button LinearLayout.LayoutParams la

  • linking to a radio button selection, asp.net c# 2011-01-20

    on the search for an answer to save the day. I have an "articles" page that holds a radiobutton list and a textarea. when the user selects a radio button, the text area is populated. now the users wants to be able to point to an article via a url lin

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.720 (s). 11 q(s)