LostFocus 处理问题
我想处理 TextBox
的 LostFocus
事件来执行一些操作。但如果 TextBox
失去焦点,我也不想执行该操作,因为单击了特殊的 Button
(打开 OpenFileDialog
)或Key.Enter
被捕获。当按下 Key.Enter
时,首先引发 KeyDown
事件。这是我的 KeyDown
事件处理程序:
public void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e != null && sender != null)
if (e.Key == System.Windows.Input.Key.Enter && !String.IsNullOrWhiteSpace(((TextBox)sender).Text))
{
e.Handled = true;
isEnterClicked = true;
((System.Windows.Controls.TextBox)sender).Visibility = System.Windows.Visibility.Collapsed;
}
}
按下 Key.Enter 后,TextBox.Visibility 发生更改,并且此操作符将引发 LostFocus 事件。
public void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
try
{
if (!isEnterClicked)
{
DependencyObject dob = (DependencyObject)sender;
while ( !(dob is ItemsControl))
{
dob = VisualTreeHelper.GetParent(dob);
}
dynamic myCmd = dob.GetValue(Control.DataContextProperty);
myCmd.SomeCommand.Execute(((TextBox)sender).GetValue(Control.DataContextProperty));
}
}
finally
{
isEnterClicked = false;
}
}
LostFocus
处理程序首先观察 isEnterPressed
是否等于 false,这意味着 TextBox
失去焦点不是因为按下了 Enter。 SomeCommand
将删除绑定到 TextBox
的某些项目,并且它会消失。
问:那么,如何对 Button.Click
事件执行相同的操作?
首先,BEFORE Button
单击后,TextBox
失去了焦点。同样的方式也是不行的。 Button.Focusable="False"
、创建新的 ControlTemplate
或处理 Timer.Elapsed
事件不满足我的要求。
I want to handle LostFocus
event of TextBox
to perform some actions. But I'm also wouldn't want to perform that actions if TextBox
lost it focus because special Button
(which opens OpenFileDialog
) was clicked or Key.Enter
was catched. When Key.Enter
was pressed, first of all KeyDown
event is raised. Here my event handler of KeyDown
:
public void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e != null && sender != null)
if (e.Key == System.Windows.Input.Key.Enter && !String.IsNullOrWhiteSpace(((TextBox)sender).Text))
{
e.Handled = true;
isEnterClicked = true;
((System.Windows.Controls.TextBox)sender).Visibility = System.Windows.Visibility.Collapsed;
}
}
After Key.Enter was pressed, TextBox.Visibility is changed, and this operator will raise LostFocus event.
public void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
try
{
if (!isEnterClicked)
{
DependencyObject dob = (DependencyObject)sender;
while ( !(dob is ItemsControl))
{
dob = VisualTreeHelper.GetParent(dob);
}
dynamic myCmd = dob.GetValue(Control.DataContextProperty);
myCmd.SomeCommand.Execute(((TextBox)sender).GetValue(Control.DataContextProperty));
}
}
finally
{
isEnterClicked = false;
}
}
LostFocus
handler firstly watch whether isEnterPressed
equals to false, its mean, TextBox
lost it focus not because enter was pressed. SomeCommand
will delete some item which was bind to TextBox
, and it will disappear.
Q: So, how to do the same with Button.Click
event?
First of all, BEFORE Button
clicked, TextBox
lost it focus. The same way is not acceptable. Button.Focusable="False"
, creating new ControlTemplate
or handling Timer.Elapsed
event do not satisfy my requirements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解问题,请尝试检查按钮是否获得焦点,如果是,则不要在文本框丢失焦点事件中执行操作。如果我是正确的,按钮应该在文本框丢失焦点事件引发之前获得焦点。
If I understood problem correctly, try to check if button is focused, if so dont perform actions in textbox lostfocus event. If Iam correct button should be focused before textbox lostfocus event raised.