I am tryting to hide textbox when checkbox value is true which I've done but when unchecked the textbox doesn't hide what can I do to fix this?
Here is my code
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;
}
}
Just use something like this:
private void checkBox_CheckChanged(object sender, RoutedEventArgs e)
{
textBox4.Visibility = (checkBox.IsChecked) ? Visibility.Visible : Visibility.Hidden;
}
Add this to the CheckChanged event like this:
checkBox.CheckedChanged += checkBox_CheckChanged;
You can try the following solution -> Binding to a WPF ToggleButton's IsChecked state
That solution basically related the check box to the content that it wants to hide on xaml using a converter instead of code behind.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With