for each cell in this column i want to bind a command from my view model , on each can execute of that command i wan't to send the "Entity" property of each item in my items source .
DataGrid :
<DataGrid ItemsSource="{Binding Items}">
<DataGridTemplateColumn Header="C.. Header" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChekced="{Binding Entity.IsIncluded, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
CommandParameter="{Binding Entity, Mode=OneWay}"
Command="{Binding DataContext.OnDicomAttributeIsIncluded, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Mode=OneWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid>
so Command and CommandParameter are bound for each CheckBox in each cell ,
The problem is that when the command is bound can execute is raised with a parameter that is null
when debugging it , it seems that CommandParameter is only bound after Command (thus the null value)
it also seems that the checkbox does not Raise can execute when CommandParameter is updated (iv'e looked using reflector and i didn't see any callback to the CommandParameter property).
iv'e done this kind of thing before with other types ItemsControls and this was never an issue , this seems like a weird behavior caused by being in a context of a CellTemplate ..
my command in the view model :
private RelayCommand<DicomAttributeInfo> _onDicomAttributeIncluded;
public RelayCommand<DicomAttributeInfo> OnDicomAttributeIsIncluded
{
get
{
if (_onDicomAttributeIncluded == null)
_onDicomAttributeIncluded = new RelayCommand<DicomAttributeInfo>(DicomAttributeIncluded);
return _onDicomAttributeIncluded;
}
}
my ICommand implementation :
public class RelayCommand<T> : ICommand
{
private Predicate<T> _canExecute;
private Action<T> _execute;
public RelayCommand(Action<T> execute, Predicate<T> canExecute = null)
{
_execute = execute;
_canExecute = canExecute;
}
private void Execute(T parameter)
{
_execute(parameter);
}
private bool CanExecute(T parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public bool CanExecute(object parameter)
{ // Here is the problem parameter is null after command is bound
return parameter == null ? false : CanExecute((T)parameter);
}
public void Execute(object parameter)
{
_execute((T)parameter);
}
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()
{
var temp = Volatile.Read(ref CanExecuteChanged);
if (temp != null)
temp(this, new EventArgs());
}
}
Any ideas?
Edit :
I could find some way to do :
_onDicomAttributeIncluded.RaiseCanExecuteChanged();
i believe this would fix the problem since the command will query all it's command parameter again and the command parameter would sample all it's bindings , it just doesn't seem plausible that it is not handled by the checkbox control under the hood (as i mentioned before when CommandParameter was changed)