WPF 事件 - 如何将 XAML 映射到基类上的事件?

发布于 2024-09-26 20:39:54 字数 381 浏览 1 评论 0原文

我正在 WPF 应用程序上的弹出窗口中使用基类。除了一件事之外,一切看起来和工作都很好:我无法将基类上的事件处理程序映射到 xaml。

当然,我可以轻松地将事件放在已实现的类上,并使用该方法来调用基类,但我只是想知道是否有一种方法可以使代码更干净,同时在一个地方实现所有通用事件处理程序并映射xamls 给他们。

示例代码:

<CommandBinding Command="Save" Executed="Save_Executed" CanExecute="Save_CanExecute" />

并在“MyClass.cs”的基类上有 Save_Executed 处理程序。

谢谢, 奥兰

I am using a base class to my Popup windows on a WPF application. Everything looks and works great beside one thing : I cant map an event handler that is on the base class to the xaml.

Ofcourse i can easily have the event on the implemented class, and use the method to call the base class, but i just wanted to know if there a way to have the code cleaner while implementing all my generic event handlers in one place and mapping the xamls to them.

Example code :

<CommandBinding Command="Save" Executed="Save_Executed" CanExecute="Save_CanExecute" />

And have Save_Executed handler on the base class of 'MyClass.cs'.

Thanks,
Oran

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

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

发布评论

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

评论(1

︶ ̄淡然 2024-10-03 20:39:54

通过不同的方法解决了我的问题...

不需要使用 xaml 映射。我刚刚为我的基类添加了一个例程,用于通过代码注册按钮事件:

        CommandBinding saveBinding = new CommandBinding(ApplicationCommands.Save, Save_Executed, Save_CanExecute);
        _saveButton.CommandBindings.Add(saveBinding);

当然,_saveButton 是实际实现的弹出窗口按钮,在初始化时传递给基类。

奥兰

Solved my issue through a different approach...

No need to use xaml mapping. i just added my base class a routine to register button events through code :

        CommandBinding saveBinding = new CommandBinding(ApplicationCommands.Save, Save_Executed, Save_CanExecute);
        _saveButton.CommandBindings.Add(saveBinding);

Ofcourse that _saveButton is the actual implmemented popup window button that is passed to the base class on initialization.

Oran

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