I have a datagrid and save button in XAML. I have a ObservableCollection bound to a datagrid.
If I add/remove a row in datagrid, I should be able to enable the 'Save' Button to allow the user to save records. However the ObservableCollection's NotifyCollectionChangedAction can't catch the 'edit' (i.e. value changes). So I want to manually enable the save button when the datagrid's currentcellchanged event is invoked (i.e. set e.CanExecute = true).
Since it's not like you can set enable=true as in WinForms, WPF has this CanExecute and Executed command binding.
In my XAML:
</UserControl.Resources>
<UserControl.CommandBindings>
<CommandBinding Command="Save" Executed="Save_Executed" CanExecute="Save_CanExecute">
</CommandBinding>
</UserControl.CommandBindings>
<Button Grid.Row="4" Content="Save" Command="Save" HorizontalAlignment="Right" Margin="5" Name="saveButton" VerticalAlignment="Center" Width="75" >
Code:
private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
{
}
private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = businessContractorViewModel != null && businessContractorViewModel.Entry != null;
}
private void businessDataGrid_CurrentCellChanged(object sender, EventArgs e)
{
//?? how to set savebutton e.canexecute = true?
}