2010-03-11 108 views
2

什麼是WinForms單選按鈕CheckedChanged的WPF等價物?WPF Radiobutton等效

我有你的基本2單選按鈕設置,其中當選擇一個文本框啓用,當另一個被選中時,它被禁用。

對於目前我使用RadioButton_Checked,除了我設置IsChecked爲xaml中的一個按鈕是true。當我引用它拋出的NullReferenceException是經過方法

編輯文本框...:

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 
    } 

回答

3

不能綁定將文本框的啓用屬性添加到xaml中相應單選按鈕的checked屬性中?

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

@Matt:我沒有完全讀完這個問題。您的解決方案是正確的選擇。 – Pwninstein 2010-03-11 04:56:55

+0

噢,...怎麼樣?就像''??? – baron 2010-03-11 05:01:31

+0

我想這樣的東西...沒有嘗試過,但可能不完全正確,我不記得確切的語法:) Matt 2010-03-11 05:08:59