栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

wpf使某个控件失去焦点

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

wpf使某个控件失去焦点

I created some textboxes and I want user to enter decimal values into them. In every application I have ever used, when I type something into the textbox and hit enter, the value is accepted and textbox lose focus. How can I do it in my app?

I know it should be relatively easy to do it with a key event, but maybe there is a command or something.

I searched the stackoverflow but I only found questions about how to keep focus after hitting enter...

解决方案

You can also create a generic behavior which can be easily applied to any textbox within your application. Here is a sample behavior class:-

public class TextBoxEnterKeyUpdateBehavior : Behavior

{

protected override void OnAttached()

{

if (this.AssociatedObject != null)

{

base.OnAttached();

this.AssociatedObject.KeyDown += AssociatedObject_KeyDown;

}

}

protected override void OnDetaching()

{

if (this.AssociatedObject != null)

{

this.AssociatedObject.KeyDown -= AssociatedObject_KeyDown;

base.OnDetaching();

}

}

private void AssociatedObject_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)

{

TextBox textBox = sender as TextBox;

if (textBox != null)

{

if (e.Key == Key.Return)

{

if (e.Key == Key.Enter)

{

textBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));

}

}

}

}

}

To use this class in your xaml, just include it in textbox behaviors collection like this :-

Here "i" refers to System.Windows.Interactivity namespace.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/907380.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号