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
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: