views:

127

answers:

1

What is the WPF equivalent for WinForms radio button CheckedChanged?

I have your basic 2 radio button set up, where when one is selected a textbox is enabled and when the other is selected it is disabled.

For the time being I was using RadioButton_Checked, except, I set IsChecked true for one button in the xaml. When I reference the textbox in that Checked method it throws NullReferenceException...

edit:

XAML:

<RadioButton Name="rb1" IsChecked="True" GroupName="1" Checked="rb1_Checked"></RadioButton>

<RadioButton Name="rb2" GroupName="1" Checked="rb2_Checked"></RadioButton>

C#:

    private void rb2_Checked(object sender, RoutedEventArgs e)
    {
        txt.IsEnabled = false;
    }

    private void rb1_Checked(object sender, RoutedEventArgs e)
    {
        txt.IsEnabled = true; //null reference here on load
    }
+1  A: 

Can't you bind the enabled property of the textbox to the checked property of the appropriate radio button in your xaml?

Matt
@Matt: I didn't read the question fully. Your solution is the right way to go.
Pwninstein
oh yeah,... how? something like `<TextBox IsEnabled={Binding rb2.Checked}/>` ???
baron
I'd think something like this... not tried it though so may not be exactly right and I can't remember the exact syntax :)<Textbox IsEnabled="{Binding ElementName=rb2, Path=IsChecked}" />
Matt
That is correct. Thanks.
baron
This is a good answer but I think it is useful to explain that the immediate cause of the exception is that `IsChecked` is set to `true` during XAML parsing before `txt` is created.
Ray Burns