mardi 14 mai 2019

trouble about SelectAll checkbox in MVVM

I have some problems to make a "SelectAll". Here's what I did by following some examples found here and there, but I must be missing something.

In my object :

        public partial class Matrix : ObservableObject
{
    public string IDMatrice { get; set; }
    private bool _IsSelectedDay;
    public bool IsSelectedDay
    {
        get
        {
            return _IsSelectedDay;
        }
        set
        {
            if (value != _IsSelectedDay)
            {
                _IsSelectedDay = value;
                RaisePropertyChanged(nameof(IsSelectedDay));
            }
        }
    }
}

ViewModel :

    private bool _AllSelectedDays;
    public bool AllSelectedDays
    {
        get
        {
            return _AllSelectedDays;
        }
        set
        {
            _AllSelectedDays = value;
            foreach (var idMatrice in this.Matrice)
            {
                idMatrice.IsSelectedDay = value;
            }
            RaisePropertyChanged(nameof(AllSelectedDays));
        }
    }

And View :

                    <Window x:Class="GET_MS.Views.WPFChangerEtablissement"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:GET_MS.ViewModels"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:mvvm="http://www.galasoft.ch/mvvmlight"
    xmlns:cuilv="clr-namespace:ClassUILibrary.Views;assembly=ClassUILibrary"
    mc:Ignorable="d"
    DataContext="{Binding ChangerEtablissement, Source={StaticResource Locator}}" 
<DataGrid Name="DgMatrice" Grid.Column="2" Margin="2" AlternatingRowBackground="LightBlue" AlternationCount="2" CanUserAddRows="False"  
                          ItemsSource="{Binding Matrice}">
                    <DataGrid.Columns>
                        <DataGridTemplateColumn>
                            <DataGridTemplateColumn.Header>
                                <CheckBox IsChecked="{Binding Path=DataContext.AllSelectedDays,RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
                            </DataGridTemplateColumn.Header>
                            <DataGridTemplateColumn.CellTemplate >
                                <DataTemplate >
                                            <CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{Binding IsSelectedDay}"/>
                                        </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>

Currently, when I go through the code step by step, I don't even enter "_IsSelectedDay", I must have a problem with the binding, I don't know. Any idea how to get me out of this? Thanks !




Aucun commentaire:

Enregistrer un commentaire