I'm trying to do a checkbox to select everything, but despite research on other topics, I don't understand why it doesn't work:
EDIT : The problem is that it looks for "SelectAllAgent" or "selectAllAgentsCommand" as a property of "Agent", which is the Source item of my DG, and not in the ViewModel. Is it possible to fix this ?
<DataGrid x:Name="DGagents" Grid.Row="1" Grid.Column="3" Grid.RowSpan="3" Margin="5" AlternationCount="2" ItemContainerStyle="{StaticResource alternatingStyle}" ItemsSource="{Binding Agents}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<CheckBox Command="{Binding SelectAllAgentCommand}"
CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}" PresentationTraceSources.TraceLevel="High"></CheckBox>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Matricule" Width="100" HeaderStyle="{StaticResource CenterGridHeaderStyle}" Binding="{Binding Matricule}" />
<DataGridTextColumn Header="Nom" Width="120" HeaderStyle="{StaticResource CenterGridHeaderStyle}" Binding="{Binding Nom}"/>
<DataGridTextColumn Header="Prénom" Width="120" HeaderStyle="{StaticResource CenterGridHeaderStyle}" Binding="{Binding Prenom}"/>
<DataGridTemplateColumn Header="Matrice" Width="150" HeaderStyle="{StaticResource CenterGridHeaderStyle}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Birthday}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Semaine" Width="60" HeaderStyle="{StaticResource CenterGridHeaderStyle}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Birthday}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Période" Width="250" HeaderStyle="{StaticResource CenterGridHeaderStyle}"/>
</DataGrid.Columns>
</DataGrid>
my checkbox bind to "IsSelected" works perfectly, but the one in the header doesn't lead to anything, I tried via a command or via a binding has a property, but nothing happens when I check it, command or property.
private bool _SelectAllAgents;
public bool SelectAllAgents
{
get
{
return _SelectAllAgents;
}
set
{
_SelectAllAgents = value;
foreach (AgentModel ag in _Agents)
ag.IsSelected = value;
RaisePropertyChanged("SelectAllAgents");
RaisePropertyChanged("Agents");
}
}
public RelayCommand SelectAllAgentCommand { get; set; }
SelectAllAgentCommand = new RelayCommand(() => selectAllAgentMethod());
private void selectAllAgentMethod()
{
if (SelectAllAgents == true)
{
SelectAllAgents = false;
}
else
{
SelectAllAgents = true;
}
}
Edit 2 : Thanks for your help ! This works :
IsChecked="{Binding Path=DataContext.SelectAllAgents,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"