3

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;
        }
    }
Wigord
  • 55
  • 7

2 Answers2

3

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;
Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52
1

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.

Community
  • 1
  • 1
aggietech
  • 978
  • 3
  • 16
  • 38