位于不同的类比主窗口的事件处理程序 [英] Event Handler located in different class than MainWindow

查看:158
本文介绍了位于不同的类比主窗口的事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

于是我也跟着在以下站点来限制一个文本框可以接受的人物的引导。

So I followed the guide on the following site to restrict the characters a textbox can accept.

http://www.rhyous.com/2010/06/ 18 /如何到涨停或-防止字符-IN-A-文本框,在CSHARP /

我的问题是我不能弄清楚如何使事件处理程序触发器在二级类。基本上,我怎么告诉VS寻找在类,而不是主窗口的事件处理程序代码?我试图寻找,但显然不知道正确的术语使用。我使用的XAML提法

My problem is I can't figure out how to make the event handler trigger in the secondary class. Basically how do I tell VS to look for the event handler code in that class instead of MainWindow? I tried searching, but apparently don't know the correct terms to use. The xaml reference I used was

xmlns:DigitBox="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"

任何想法?

推荐答案

您是关闭使用命令和命令绑定好得多。我不知道具体是什么命令,将将绑定到您想要的功能,一个文本框,但对WPF的目标之一是减轻后面代码中使用事件处理程序。

You are much better off using commands and command bindings. I'm not sure what the specific command that would would bind to for a text box for your desired functionality, but one of the goals for WPF was to lessen the use of Event Handlers in code behind.

查看此文章的概述命令和本文一个方式挂钩的命令的事件。 WPF指挥是最酷的功能之一,使的UI和业务逻辑之间的关注真正的分离。

Check out this article for an overview of commands and this article for a way to hook up commands with events. WPF commanding is one of the coolest features to enable true separation of concerns between UI and business logic.

至于最坏的情况下的解决方案,你可以创建自己的文本框从文本框控件继承和挂钩在该类事件。然后,您的控制将是可重复使用的。

As a worst case scenario solution, you could create your own text box that inherits from the text box control and hook up the events in that class. Your control would then be reusable.

这篇关于位于不同的类比主窗口的事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆