views:

62

answers:

1

I have a WPF form with 3 buttons and have routed events on them, commands are binded on start...

 private void InitCommandBinding(UIElement frameworkElement) {
   CommandBinding commandBinding;





   commandBinding = new CommandBinding(ViewModelCommands.Save, Save_Executed, Save_CanExecute);
   frameworkElement.CommandBindings.Add(commandBinding);

   commandBinding = new CommandBinding(ViewModelCommands.SaveAndClose, SaveAndClose_Executed, SaveAndClose_CanExecute);
   frameworkElement.CommandBindings.Add(commandBinding);

   commandBinding = new CommandBinding(ViewModelCommands.Delete, Delete_Executed, Delete_CanExecute);
   frameworkElement.CommandBindings.Add(commandBinding);
  }

the details ui has code like

 private void Delete_Executed(object sender, ExecutedRoutedEventArgs e) {
   try
   {do validations }
}


private void Delete_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
   e.CanExecute = viewModel.IsValid(); (returns bool)
  }

Validity enables and disables buttons etc.

The form has an instance of an object new or old and validations take place on the data

My issue is that the event just excute all the time and the form just hangs cause validation code does poll db etc to check....

how to I just get them to fire once when the form is loaded mmm....

+2  A: 

If I understand you well it is only necessary to check the validity of the data at form load and the IsValid method is resource intensive? Why don't you change the IsValid() method to an IsValid property and set this is in the Form_Loaded event?

The CanExute method will be checked any time the UI fires an event like TextChanged, LostFocus etc. So you better make such methods very lightweight.

Dabblernl
thanks for the lead, I guess the CanExute fires a lot and I have validation code like check if code exists in db etc that poll the db.The validation framework services handle that but the ui level problems arose...
abmv
Spot-on answer. CanExecute needs to be quick. Checking the DB, hitting a web service, etc. not a good idea here. You should only check the DB when the user is ready to commit their changes.
Drew Marsh