mercredi 27 décembre 2017

Binding checkbox issue in WPF MVVM

I am facing a problem in getting the value from VM for checkbox IsChecked binding value. (I'm using MVVM Light).

My issue: When checkbox IsChecked is changed, it is not firing back to my VM property that I bind to.

Below is the code.

I have a class with boolean values (in a class file).

public class Rights
{
    public bool bSales { get; set; }
    public bool bProduct { get; set; }
    public bool bZone { get; set; }
    public bool bPercentage { get; set; }
    public bool bUser { get; set; }
}

And this is the property that my checkboxes will bind to (in VM).

private Rights user_Rights;
public Rights User_Rights
{
    get { return user_Rights; }
    set { Set(ref user_Rights, value); }
}

And below is the property for my 'Select All' check box (in VM).

private bool? rights_All;
public bool? Rights_All
{
    get { return rights_All; }
    set
    {
        Set(ref rights_All, value);

        if (value == true)
        {
            User_Rights = new Rights() { bSales = true, bProduct = true, bPercentage = true, bZone = true, bUser = true };
        }
        else if(value == false)
        {
            User_Rights = new Rights() { bSales = false, bProduct = false, bPercentage = false, bZone = false, bUser = false };
        }
    }
}

And finally, below is my XAML for the binding.

<CheckBox Content="Sales PIC" IsChecked="{Binding User_Rights.bSales,Mode=TwoWay}" />
<CheckBox Content="Product" IsChecked="{Binding User_Rights.bProduct,Mode=TwoWay}" />
<CheckBox Content="Zone" IsChecked="{Binding User_Rights.bZone,Mode=TwoWay}" />
<CheckBox Content="Percentage" IsChecked="{Binding User_Rights.bPercentage}" />
<CheckBox Content="User" IsChecked="{Binding User_Rights.bUser}" />
<CheckBox Content="Select All" IsChecked="{Binding Rights_All}" />

Here is what I am doing in picture. enter image description here

Any suggestion on where did I do wrong? Thanks.




Aucun commentaire:

Enregistrer un commentaire