处理未选中的复选框

时间:2012-10-04 09:17:07

标签: c# wpf

我正在尝试创建一个方法,用于检查我的复选框是否未被选中,如果是,它将显示一个消息框,然后运行我的循环,应该将我的所有部门设置为不显示,但是我可以我似乎得到了我的代码来检测复选框何时被取消,它只是命中第一个if语句,即使未选中复选框也将其设置为false,也不会进一步。

    private void displayOnWebsite_Checked(object sender, RoutedEventArgs e)
    {
        CheckBox displayOnWebsite = sender as CheckBox;

        if (displayOnWebsite.IsChecked == false)
        {
            var departments = model.Name;
            var departmentChildren = model.Children;
            var messagebox = System.Windows.MessageBox.Show("Do you wish to hide all sub deparments and products.",
                 "List of Box",
                 MessageBoxButton.YesNo);

            if (!session.IsConnected)
                session.Reconnect();

            // Used to hide child departments and products.
            if (messagebox == MessageBoxResult.Yes)
            {
                if (departmentChildren != null)
                {
                    int zeroChildren = 0;

                    if (departmentChildren.Count.Equals(zeroChildren))
                    {
                        foreach (Department children in departmentChildren)
                            children.IsVisibleOnWebsite = false;
                    }
                }
            }
            else
                System.Windows.MessageBox.Show("Nevermind then...");
        }
        else
            System.Windows.MessageBox.Show("nothing happened here");
    }

WPF:

                        <CheckBox IsChecked="{Binding IsVisibleOnWebsite}" Name="displayOnWebsite" Style="{StaticResource Label}" Checked="displayOnWebsite_Checked">Display On Website</CheckBox>

1 个答案:

答案 0 :(得分:2)

使用“未选中”事件来处理取消选中。