I have radio button and I want to define binding between the radio button IsChecked state and the visibility some stackpanel so I wrote this convert method:
public class RadioBtnState2Visible : IValueConverter
{
// RadioBtn start => Visible / Hide
public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
{
if( value == null )
return Visibility.Collapsed;
bool visibility = false;
bool.TryParse( value.ToString(), out visibility );
return visibility ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
{
throw new NotImplementedException();
//return ( ( Visibility )value ) == Visibility.Visible ? true : false;
}
}
The xaml is
<local:PageEx.Resources>
<local:RadioBtnState2Visible x:Key="Convert" />
</local:PageEx.Resources>
<RadioButton x:Name="MyRadioBtn1" GroupName="group1" />
<RadioButton x:Name="MyRadioBtn2" GroupName="group1" />
<StackPanel Visibility="{Binding ElementName=MyRadioBtn1, Path=IsChecked, Converter={StaticResource Convert}}" />
Now, I dont see any convert fire – i dont see that the application stops on the breakpoint that i set on the first line of the convert method.
This is a duplicate of your previous question, already answered. Here is the answer again:
I got your code working in a test project with one minor change. I made the converter a page resource with the following syntax:
I should also mention that I had to actually put something in the stackpanel as well to see the change as by default it collapses to nothing 🙂 I assume you actually have content in your real stackpanel.
Just to explain what is happening here. By adding “local:” to the resource declaration you are actually enclosing an instance of a Resource, and not changing the current resource. Referencing it by “Convert” does not work because it cannot find the resource where it expects to find it.