2015-12-21 140 views
2

我正在嘗試隱藏文本框,當checkbox值爲true時,我已經完成了,但未選中時文本框不隱藏我可以做些什麼來解決這個問題?WPF,未選中時隱藏文本框

這裏是我的代碼

private void textBox4_TextChanged(object sender, TextChangedEventArgs e) 
    { 
    } 

    private void checkBox_Checked(object sender, RoutedEventArgs e) 
    { 
     Handle(sender as CheckBox); 
    } 
    private void checkBox_Unchecked(object sender, RoutedEventArgs e) 
    { 
     Handle(sender as CheckBox); 
    } 

    void Handle(CheckBox checkBox) 
    { 
     bool chkd = checkBox.IsChecked.Value; 

     if (chkd) 
     { 
      textBox4.Visibility = Visibility.Visible; 
     } 
     else 
     { 
      textBox4.Visibility = Visibility.Hidden; 
     } 
    } 
+0

你如何訂閱檢查/取消選中事件? – Valentin

+0

您不......使用XAML/WPF,您可以將IsChecked屬性綁定到視圖模型中的屬性。 – aggietech

回答

2

只要使用這樣的事情:

private void checkBox_CheckChanged(object sender, RoutedEventArgs e) 
{ 
    textBox4.Visibility = (checkBox.IsChecked) ? Visibility.Visible : Visibility.Hidden; 
} 

一下添加到CheckChanged事件是這樣的:

checkBox.CheckedChanged += checkBox_CheckChanged;