2 pièce(s) jointe(s)
une checkbox SelectAll en MVVM ?
Bonjour,
j'ai quelques soucis pour créer, comme dit le texte, une checkbox qui me permet de "selectionner tout" dans un modele MVVM.
Après avoir cherché, j'ai un résultat qui ne fonctionne pas, et je me demandais si vous pouviez m'aider à ce sujet;
Mon objet :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 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));
}
}
}
} |
Si j'ai bien compris, je dois rajouter une propriété pour la selection, comme ceci.
Ensuite, dans le ViewModel :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| private bool _AllSelectedDays;
public bool AllSelectedDays
{
get
{
return _AllSelectedDays;
}
set
{
_AllSelectedDays = value;
foreach (var idMatrice in this.Matrice)
{
idMatrice.IsSelectedDay = value;
}
RaisePropertyChanged(nameof(AllSelectedDays));
}
}
private ObservableCollection<Matrix> _Matrice;
public ObservableCollection<Matrix> Matrice
{
get
{
return _Matrice;
}
set
{
if (value != _Matrice)
{
_Matrice = value;
RaisePropertyChanged(nameof(Matrice));
}
}
}
private Matrix _SelectedMatrice;
public Matrix SelectedMatrice
{
get
{
return _SelectedMatrice;
}
set
{
if (value != _SelectedMatrice)
{
_SelectedMatrice = value;
RaisePropertyChanged(nameof(SelectedMatrice));
}
}
} |
Et enfin, dans le WPF :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <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> |
Ce que j'obtiens, ce sont des combobox qui n'ont aucun effet quand je les coches :
Pièce jointe 475082
(je ne rentre même pas dans le "selectedAllDays")
et une nouvelle colonne dans ma DataGrid du coup :
Pièce jointe 475087
Qui a un effet étrange. Si je coche la case 2 ici, ça me cochera la case 1 tout à gauche. Et ainsi de suite : 3 => 2, 4 => 3...
Edit : c'est réparé, si je selectionne une des cases, je passe bien par "IsSelectedDay", de ce côté là ça roule. Par contre, je ne passe jamais dans "AllSelectedDay", et là je bloque...
Une idée pour me sortir de là ? :)