WPF Checkbox check IsChecked

后端 未结 8 799
别跟我提以往
别跟我提以往 2020-12-08 10:25

I\'m not talking about an event handler for this, but rather a simple If Statement checking if the CheckBox has been checked. So far I have:

<
相关标签:
8条回答
  • 2020-12-08 10:34

    IsChecked property of CheckBox is Nullable boolean.

    public bool? IsChecked { get; set; }
    

    Create a Nullable boolean and equate it will work for you.

    Code

    bool? NullableBool = chkRevLoop.IsChecked;
    if(NullableBool == true)    {    }
    
    0 讨论(0)
  • 2020-12-08 10:38

    You have to do this conversion from bool? to bool, to make it work:

    if((bool)(chkRevLoop.IsChecked)){}
    

    Since it is already a bool condition you need not to put true false because if it is true then only it will come inside this if condition else not. so, no need to even put chkRevLoop.IsChecked == true here, you are by default asking ==true by puttin IsChecked

    0 讨论(0)
  • 2020-12-08 10:39

    Consider checking if the property has a value:

    var isChecked = chkRevLoop.IsChecked.HasValue ? chkRevLoop.IsChecked : false;
    
    if (isChecked == true){}
    
    0 讨论(0)
  • 2020-12-08 10:42

    You can use null coalescing operator. This operator returns right-hand operand if the left-hand operand is null. So you can return false when the CheckBox is in indeterminate state (when the value of IsChecked property is set to null):

    if (chkRevLoop.IsChecked ?? false)
    {
    
    }
    
    0 讨论(0)
  • 2020-12-08 10:51

    One line is enough to check if the radio button is checked or not:

    string status = Convert.ToBoolean(RadioButton.IsChecked) ? "Checked" : "Not Checked";
    
    0 讨论(0)
  • 2020-12-08 10:51

    You should use Nullable object. Because IsChecked property can be assigned to three different value: Null, true and false

    Nullable<bool> isChecked  = new Nullable<bool>(); 
    isChecked = chkRevLoop.IsChecked; 
    
    if (isChecked.HasValue && isChecked.Value)
    {
    
    
    }
    
    0 讨论(0)
提交回复
热议问题