您的位置:首页 > 其它

重写Windows基类,自定义WPF窗口,实现改回车键为TAB

2011-02-19 22:31 302 查看
在WinForm时,可以定义一个基类继承自Form,从而在基类中重写和添加功能,要在WPF中实现类似方法要分为三步:

1. 自定义一个基类MyWindow继承自Window.

2.  将窗口的CS继承自MyWindow。

3. 在XAML中引用MyWindow命名空间,并在使用其别名自定义WPF窗口。

如下例重写Windows基类,自定义WPF窗口,实现改回车键为TAB:

自定义基类

using System.Windows;
using System.Windows.Input;

namespace DecorationMS.WindowsBase
{
public class DMSbase : Window
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// MoveFocus takes a TraveralReqest as its argument.
TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);

// Gets the element with keyboard focus.
UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;

// Change keyboard focus.
if (elementWithFocus != null)
{
elementWithFocus.MoveFocus(request);
}
e.Handled = true;
}
base.OnKeyDown(e);
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: