I have a checkbox on a grid column that has a command assigned to it. My CanExecute method is firing when I load the window and for each item in the grid even if I haven't clicked the check box. I end up with a loop of messagebox dialogs to close.
It doesn't seem to fire as often if the focus is not lost, but it's still firing before I click a box.
Ideally I'd like for it not to run at all unless I check the checkbox. I'm using the DelegateCommand from Microsoft.VisualStudio.Utilities.
<DataGrid ItemsSource="{Binding Employees}" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Register">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Command="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.CheckboxRegisterCommand}"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
And the viewmodel:
Public Property CheckboxRegisterCommand As DelegateCommand
Public Sub New()
CheckboxRegisterCommand = New DelegateCommand(AddressOf Register, AddressOf CanRegister)
Employees = New ObservableCollection(Of Employee) From
{
New Employee(),
New Employee(),
New Employee()
}
End Sub
Private Function CanRegister() As Boolean
MessageBox.Show("Can Register..")
Return True
End Function
Private Sub Register()
MessageBox.Show("Registering")
End Sub