如何在WPF TextBox上强制TextChanged事件并保持焦点? [英] How to force a TextChanged event on a WPF TextBox and mantain the focus?

查看:242
本文介绍了如何在WPF TextBox上强制TextChanged事件并保持焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 WPF 中创建一个数字文本框,其中有两个按钮可以增加或减少该值。

I am creating a numeric TextBox in WPF, with two buttons to increase and decrease the value.

我已经创建了两个 RoutedCommand 来管理行为,它们运行良好。我只想解决一个问题。我希望控件在执行增加或减少命令时通知绑定到其 TextProperty 的所有对象。

I have create two RoutedCommand to manage the behiavor and they are working well. There is only one problem that I would like to solve. I would like that the control notifies all object binded to its TextProperty when an increase or decrease command is executed.

通知仅当我将焦点更改为另一个控件时

任何帮助都表示感谢,
谢谢

Any help is really appreciated, Thank you

推荐答案

在绑定和 TextChanged中使用 UpdateSourceTrigger = Explicit 事件更新 BindingSource
,所以您正在写类似这样的东西:

Use UpdateSourceTrigger="Explicit" in the Binding and in TextChanged event update the BindingSource. so you are writing something like this:

<NumericTextBox x:Name="control" Text={Binding Path=MyProperty}/>

反而这样做

<NumericTextBox x:Name="control" Text={Binding Path=MyProperty, UpdateSourceTrigger=Explicit}/>

并在 TextChanged 事件处理程序中更新绑定

and in TextChanged event handler update the binding.

control.GetBindingExpression(NumericTextBox.TextProperty).UpdateSource();

就这样完成了。
希望对您有所帮助!

and thats done. Hope it helps!!

这篇关于如何在WPF TextBox上强制TextChanged事件并保持焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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