'C# WPF CheckBox Command not Binded

View:

 <DataGridTemplateColumn.Header>
     <CheckBox x:Name="chk_Top" HorizontalAlignment="Center" HorizontalContentAlignment="Center"
         Command="{Binding Chk_GridTop}"/>
 </DataGridTemplateColumn.Header>

View Model:

public partial class ViewModel_AC: INotifyPropertyChanged
{
    ICommand _chkGridTop;

    public ICommand Chk_GridTop
    {
        get { return _chkGridTop ?? (_chkGridTop = new DelegateCommand(_chk_GridTop)); }
    }

    public void _chk_GridTop(object check)
    {
     //Empty
    }
}

DelegateCommand

public class DelegateCommand : ICommand
{
    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new NullReferenceException("execute can no null");

        _execute = execute;
        _canExecute = canExecute;
    }

    public DelegateCommand(Action<object> execute) : this(execute, null)
    {

    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        _execute.Invoke(parameter);
    }
}

There are other Commands bind with buttons which working well, but checkBox command is not working

I want to fire Chk_GridTop when checkBox is checked or unchecked

Is it something wrong what i use that?



Solution 1:[1]

add CommandParameter

 <DataGridTemplateColumn.Header>
            <CheckBox x:Name="chk_Top" HorizontalAlignment="Center" HorizontalContentAlignment="Center"
         Command="{Binding Chk_GridTop}" CommandParameter="{Binding RelativeSource={RelativeSource Self},Path=IsChecked}"/>
        </DataGridTemplateColumn.Header>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sync