CheckBox,IsChecked == false不起作用

时间:2012-08-09 09:49:01

标签: c# wpf

private void checkBox1_Checked(object sender, RoutedEventArgs e)
{
    if (checkBox1.IsChecked == false)
    {
        grid1.Visibility = System.Windows.Visibility.Hidden;
    }

    if (checkBox1.IsChecked == true)
    {
        grid1.Visibility = Visibility;
    }

}

为什么grid1.Visibility = System.Windows.Visibility.Hidden;无效,网格未隐藏

5 个答案:

答案 0 :(得分:5)

您正在处理Checked事件,该事件仅在选中CheckBox时发生,而不是在未选中时发生。要解决此问题,请同时处理Unchecked event

private void checkBox1_Checked(object sender, RoutedEventArgs e) 
{ 
    grid1.Visibility = System.Windows.Visibility.Visible; 
} 

private void checkBox1_Unchecked(object sender, RoutedEventArgs e) 
{ 
    grid1.Visibility = System.Windows.Visibility.Hidden; 
} 

答案 1 :(得分:2)

当不再选中该复选框时,请使用Unchecked事件通知。

答案 2 :(得分:0)

试试这个,你必须使用折叠

private void checkBox1_Checked(object sender, RoutedEventArgs e)
{
        grid1.Visibility = System.Windows.Visibility.Visible;
}

private void checkBox1_UnChecked(object sender, RoutedEventArgs e)
{

        grid1.Visibility = System.Windows.Visibility.Collapsed;
}

答案 3 :(得分:0)

试用此代码

protected void checkBox1_Unchecked(object sender, RoutedEventArgs e)
 {
     grid1.Visibility = System.Windows.Visibility.Hidden;
 }

protected void checkBox1_Checked(object sender, RoutedEventArgs e)
  {
     grid1.Visibility = System.Windows.Visibility.Visible; 
  }

答案 4 :(得分:-1)

 private void checkBox1_CheckStateChanged(object sender, RoutedEventArgs e)
    {
        if (checkBox1.Checked)
        {
            grid1.Visibility = System.Windows.Visibility.Hidden;
        }else
        {
            grid1.Visibility = System.Windows.Visibility.Visible;
        }

    }

IsChecked没有这样的属性。