简体   繁体   English

WPF绑定复选框bool?

[英]WPF Bind checkbox to bool?

I have a WPF checkbox binded to ViewModel nullable boolean property. 我有一个绑定到ViewModel可以为空的布尔属性的WPF复选框。 I am setting this property to false or true in Constructor, to avoid Interminent state but no matter what I do the Initial state of checkbox stays grayed. 我在构造函数中将此属性设置为false或true,以避免Interminent状态,但无论我做什么,复选框的初始状态保持灰色。 The binding working just fine since once I change the state by clicking the checkbox on UI I am getting controls values (true/false). 绑定工作正常,因为一旦我通过单击UI上的复选框更改状态,我将获得控件值(true / false)。 Any Ideas? 有任何想法吗?

XAML: XAML:

<CheckBox Margin="0,4,0,3"
          VerticalAlignment="Center"
          Content="Mutual"
          IsChecked="{Binding MutualChb}" />

ViewModel: 视图模型:

public ContstrutorViewModel()
{
    MutualChb = true;
}

private bool? _mutualChb;
public bool? MutualChb
{
    get { return _mutualChb; }
    set
    { 
        _mutualChb = value; 
        _mutualChb = ( _mutualChb != null ) ? value : false;
        OnPropertyChanged("MutualChb");
    }
}

The reason for that is because it's initially null. 原因是因为它最初为空。

private bool? _mutualChb;
public bool? MutualChb
{
    get { return (_mutualChb != null ) ? _mutualChb : false; }
    set
    { 
        _mutualChb = value;               
        OnPropertyChanged("MutualChb"); 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM