WPF 路由事件触发?

发布于 2024-08-08 19:23:04 字数 1077 浏览 1 评论 0原文

我有一个带有 3 个按钮的 WPF 表单,并在它们上路由了事件,命令在启动时绑定...

 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)
  }

有效性启用和禁用按钮等。

该表单有一个新或旧对象的实例,并且对数据进行验证

我的问题是事件一直执行并且表单只是挂起导致验证代码确实轮询数据库等来检查....

如何在加载表单时让它们触发一次嗯....

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....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海拔太高太耀眼 2024-08-15 19:23:05

如果我理解你的话,只需要在表单加载时检查数据的有效性,并且 IsValid 方法是资源密集型的吗?
为什么不将 IsValid() 方法更改为 IsValid 属性并将其设置在 Form_Loaded 事件中?

每当 UI 触发 TextChanged、LostFocus 等事件时,都会检查 CanExute 方法。因此,您最好使此类方法变得非常轻量级。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文