1

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;
    }
}

enter image description here

enter image description here

Edit 2 : Thanks for your help ! This works :

     IsChecked="{Binding Path=DataContext.SelectAllAgents, 
                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" 
Sephystos
  • 175
  • 1
  • 9
  • 2
    Chances are the `DataContext` at the point that you do `{Binding SelectAllAgentCommand}` isn't what you think it is? So the binding to `SelectAllAgentCommand` is failing. You should see a message about this in the Output window: if not, go to Debug -> Options -> Debugging -> Output Window -> WPF Trace Settings -> Data Binding and set it to something high – canton7 Feb 17 '20 at 09:10
  • 1
    A)The `CheckBox` inside the `DataTemplate` must use `RelativeSource` inside its binding B)`SelectAllAgentCommand` must be public property, as well as the property you binding `IsChecked` to – styx Feb 17 '20 at 09:13
  • It's a public property, and the Checkbox inside the DataTemplate works at the moment, should i change something ? I'm looking for the datacontext. – Sephystos Feb 17 '20 at 09:15
  • Nothing in the output. I don't know why every binding are ok except this one... Nothing change if im doing a classic binding (isChecked => selecteAllAgents) – Sephystos Feb 17 '20 at 09:23
  • does [this](https://stackoverflow.com/questions/48955781/wpf-select-all-checkbox-in-a-datagrid) answer your question? – styx Feb 17 '20 at 09:25
  • Unless you have an animation for your datagrid, then the name is redundant, also instead of marking everything in property setter use the `selectAllAgentMethod()`. DataContext of your ` – XAMlMAX Feb 17 '20 at 09:27
  • Yes it's because I'm trying a few things to understand why it's doesn't works. I don't know what is wrong with my dataContext, if i press F12 on "SelectAllAgentCommand", everything seems ok with this bind, i swap on the ViewModel. And i'm looking for your solution styx, seems a little more complex but i'll try. – Sephystos Feb 17 '20 at 09:36
  • The fact that you can see the items when you press F12 doesn't mean that the context is correct. That's just the VS working out where stuff is. Is your DG in a DataTemplate? Also to get the idea of what is going on use this `PresentationTraceSources.TraceLevel=High` and see what the output window shows. – XAMlMAX Feb 17 '20 at 10:12
  • "BindingExpression path error: 'SelectAllAgentCommand' property not found on 'object' ''CheckBox' (Name='')'. BindingExpression:Path=SelectAllAgentCommand; DataItem='CheckBox' (Name=''); target element is 'CheckBox' (Name=''); target property is 'Command' (type 'ICommand')" ok, this is the problem if i understand correctly – Sephystos Feb 17 '20 at 10:20
  • Yes, the DataContext of your column is not set. And from what I can see is that your first column hasn't got Bidning set. Maybe you should set that to `{Binding .}` and see if that works? – XAMlMAX Feb 17 '20 at 10:31
  • Only the header of the first column, every else comboboxes on the first columns are ok. Same error with a "IsChecked="{Binding SelectAllAgents}"" – Sephystos Feb 17 '20 at 10:47
  • Ok, 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? – Sephystos Feb 17 '20 at 10:59
  • 1
    Yes, you need to use relative source for your binding and point it to an element that has correct data context. You can use a Name if you want to and point to data context. So let's say you have a window or user control or stack panel, name it "Host" and then use `ElementName=Host, Path=DataContext.selectAllAgentsCommand`. – XAMlMAX Feb 17 '20 at 11:04

0 Answers0