Bind StackPanel Visiblility to Radiobutton IsChecked

First steps with Silverlight …

The context :

I have some radiobuttons, and a stackpanel I want to be visible if one or the radiobutton is checked

The solution I found :

1) Write the code

The radio buttons


// ...

<RadioButton Name="SomeRadio"  IsChecked="True" Content="some radio/>
 <RadioButton Name="WantedRadio" Content="the wanted radio"/>

// ...

The StackPanel


// ...

<StackPanel Name="ThePanel">

// some content

</StackPanel>

2) Convert

Since the Visibility property type  is an enum and the Ischeckd a boolean, we need a converter

Create it : you must implement the IValueConverter Inerface which is in the “System.Windows.Data” namespace


using System;
using System.Windows;
using System.Windows.Data;

namespace TheNamespace
{
 public class VisibilityConverter : IValueConverter
 {
 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
 {
// just in case
if (value == null)
{
return Visibility.Collapsed;
}

bool ischecked = System.Convert.ToBoolean(value);
return ischecked ? Visibility.Visible : Visibility.Collapsed;
 }

// had to be implemented
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
 }
}

Reference the namespace of the converter in the xaml file


xmlns:local="clr-namespace:TheNamespace"

Add the class as ressource for the user control

<UserControl.Resources>

 <local:VisibilityConverter x:Key="VisibilityConverter" />
</UserControl.Resources>

And now you can use it to bind the properties


Visibility="{Binding IsChecked,  ElementName=WantedRadio, Converter={StaticResource  VisibilityConverter}}">

Incoming search terms:

  • binding visibility to radiobutton
  • asp bind textbox visiblility to radiobutton
  • vnext radio button null
  • visibility converter bound to radio button ischecked property windows phine
  • stackpanel visibility binding
  • silverlight binding null radio buttons
  • radiobutton change visibility stackpanel
  • how to bind radio button in with windows phone
  • Bind visibility to radiobutton checked
  • bind coontrol visibility to radion button