2

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?

        }
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Kiddo
  • 5,052
  • 6
  • 47
  • 69

1 Answers1

0

i added a trigger, when i finish editing cell, set the bool Edited= true and postback, the save button will catch the change and set itself enable.

i don't know if it's the best, but it works for me.

private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = Edited;
        }

 private void businessDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            Edited = true;
        }
Kiddo
  • 5,052
  • 6
  • 47
  • 69