这听起来像你只是想执行你的计算时文本框有引发LostFocus
在你看来,你将有
<Window...>
<TextBox LostFocus="Perform_Calculation"/>
</Window>
在
然后代码隐藏定义方法
private void Perform_Calculation(object sender, RoutedEventArgs e)
{
// calculation logic here...
// probably also where you will check if the Text is empty (nothing to be done)
}
或者,您可以使用TwoWay绑定和th e default PropertyChangeTrigger on TextBox应该是LostFocus - 所以你应该只在用户离开TextBox的时候得到属性更新。
注意,这将无法满足的也处理得上的按Enter键你的希望,为文本框将仍然具有焦点。若要启用此功能,请稍微扩展它
<Window...>
<TextBox LostFocus="Perform_Calculation" KeyDown="TextBox_KeyDown"/>
</Window>
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
// perform calculation here (probably method that you will also call
// from LostFocus handler to avoid duplication)
}
}
我猜用户将无法通过按来结束编辑? –
应该更新答案以提供此功能的扩展。 –