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

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

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

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

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

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

ContractedBlock.gif ExpandedBlockStart.gif XAML
 
   
< DecorationMS:DMSbase x:Class ="DecorationMS.MainWindow"
xmlns:DecorationMS
="clr-namespace:DecorationMS.WindowsBase"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
Title
="TEST" Height ="700" Width ="1000" >
< Grid Name ="MainGrid" >

</ Grid >
</ DecorationMS:DMSbase >
ContractedBlock.gif ExpandedBlockStart.gif CS
 
   
public partial class MainWindow : WindowsBase.DMSbase
{
public MainWindow()
{
InitializeComponent();

}
}
ContractedBlock.gif ExpandedBlockStart.gif 自定义基类
 
   
1 using System.Windows;
2 using System.Windows.Input;
3
4 namespace DecorationMS.WindowsBase
5 {
6 public class DMSbase : Window
7 {
8 protected override void OnKeyDown(KeyEventArgs e)
9 {
10 if (e.Key == Key.Enter)
11 {
12 // MoveFocus takes a TraveralReqest as its argument.
13 TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
14
15 // Gets the element with keyboard focus.
16 UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
17
18 // Change keyboard focus.
19 if (elementWithFocus != null )
20 {
21 elementWithFocus.MoveFocus(request);
22 }
23 e.Handled = true ;
24 }
25 base .OnKeyDown(e);
26 }
27
28
29 }
30 }

转载于:https://www.cnblogs.com/Laro/archive/2011/02/19/1958715.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值