jeudi 19 octobre 2017

Binding CheckBoxes to Array of Booleans

I have 15 CheckBoxes that I want to assign their IsChecked property to an array of booleans. Here is how I am doing this so far:

C#

private bool?[] _boolArray = new bool?[15];
public bool?[] BoolArray
{
    get => _boolArray;
    set
    {
        if (_boolArray == value) return;
        _boolArray = value;
        OnPropertyChanged(nameof(BoolArray));
    }
}

XAML

<CheckBox Grid.Row="2" Grid.Column="1" IsChecked="{Binding Path=BoolArray[0]}"/>
<CheckBox Grid.Row="3" Grid.Column="1" IsChecked="{Binding Path=BoolArray[1]}"/>
<CheckBox Grid.Row="4" Grid.Column="1" IsChecked="{Binding Path=BoolArray[2]}"/>
<CheckBox Grid.Row="5" Grid.Column="1" IsChecked="{Binding Path=BoolArray[3]}"/>
<CheckBox Grid.Row="6" Grid.Column="1" IsChecked="{Binding Path=BoolArray[4]}"/>

...

Now I go and get a property that has all of the true and false values for this Person:

using (var context = new DashboardContext())
{
    var privilege = context.Privileges.FirstOrDefault(x => x.UserId == userId && x.ProgramId == programId);
    if (privilege != null)
    {
        SelectedPrivilegeString = privilege.ProgramText;

        BoolArray[0] = privilege.Priv1;
        BoolArray[1] = privilege.Priv2;
        BoolArray[2] = privilege.Priv3;
        BoolArray[3] = privilege.Priv4;
        BoolArray[4] = privilege.Priv5;
        BoolArray[5] = privilege.Priv6;
        BoolArray[6] = privilege.Priv7;
        BoolArray[7] = privilege.Priv8;
        BoolArray[8] = privilege.Priv9;
        BoolArray[9] = privilege.Priv10;
        BoolArray[10] = privilege.Priv11;
        BoolArray[11] = privilege.Priv12;
        BoolArray[12] = privilege.Priv13;
        BoolArray[13] = privilege.Priv14;
        BoolArray[14] = privilege.Priv15;
    }
}
OnPropertyChanged(nameof(TextArray));

The issue I have with this method is that regardless of the value in the BoolArray, whether it be true or false the CheckBox always stays null. How can I modify this so that the bindings are updated correctly?




Aucun commentaire:

Enregistrer un commentaire