How do I make a textbox visible and hidden with a checkbox?

前端 未结 3 900
甜味超标
甜味超标 2020-12-22 03:51

So I am trying to make my textbox invisible when a checkbox is not checked. Everything works fine untill I check the box and then uncheck it again. The textbox will stay vis

3条回答
  •  自闭症患者
    2020-12-22 04:10

    when CheckBox goes to unchecked state, Unchecked event fires (simmetric to Checked). Add event handler to both of them.

    
    
    private void chbon_Checked_1(object sender, RoutedEventArgs e)
    {
         txtshow.Visibility = cchbon.IsChecked ? Visibility.Visible : Visibility.Hidden;
    }
    

    It is common to use binding to boolean property to set Visibility of some element. There is a BooleanToVisibilityConverter in .NET, which returns Visible for true, and Collapsed for false. Collapsed is different from Hidden: Hidden element stil claims the space on the screen as if it was Visible.

    There is a way to achieve everything in XAML using a Trigger:

    
    
        
                
                    
           
    
    

提交回复
热议问题