开发者

WPF Routed events firing?

I have a WPF form with 3 buttons and have routed开发者_如何学JAVA 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....


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜