baron
baron

Reputation: 11191

WPF Radiobutton equivalent

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
    }

Upvotes: 2

Views: 1932

Answers (1)

Matt
Matt

Reputation: 3014

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

<Textbox IsEnabled="{Binding ElementName=rb2, Path=IsChecked}" />

Upvotes: 3

Related Questions