WPF DatePicker LostFocus触发7次 [英] WPF DatePicker LostFocus fires seven times

查看:216
本文介绍了WPF DatePicker LostFocus触发7次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里很简单。请看布局:

 < Grid> 
< Grid.RowDefinitions>
< RowDefinition>< / RowDefinition>
< RowDefinition>< / RowDefinition>
< /Grid.RowDefinitions>
< TextBox Grid.Row =0>< / TextBox>
< DatePicker Grid.Row =1
Name =_ datePicker
LostFocus =_ datePicker_OnLostFocus>< / DatePicker>
< / Grid>

和codebehind:

  private void _datePicker_OnLostFocus(object sender,RoutedEventArgs e)
{
Debug.WriteLine(LostFocuse);
}

所以,麻烦的是当我拿起一些日期点击 TextBox 然后,事件 LostFocus 触发7(七!)次。当 DatePicker 真的失去了焦点,当我在 TextBox ,而剩下的六次完全不能为我解释。 p>

我该如何解决?我只需要一个触发此事件。还是可以使用一些其他的事件?我尝试了 LostKeyBoardFocus ,结果相同。

解决方案

LostFocus是一个路由事件,路由策略设置为 Bubble 。通过泡泡,它意味着它会冒泡到它的父母直到根窗口,直到通过明确设置 e.Handled = true; 来处理。

所以,这意味着即使 ,当孩子控制松动的焦点时,它会浮起来你的datePicker,这就是为什么你看到多个点击你的方法



您可以检查属性 IsKeyboardFocusWithin ,如果焦点在您的控制。由于您对收听失落焦点事件不感兴趣,您可以在此处理程序中检查此属性,并且仅在datePicker丢失实际焦点时执行代码:

  private void _datePicker_OnLostFocus(object sender,RoutedEventArgs e)
{
DatePicker选择器=发件人为DatePicker;
if(!picker.IsKeyboardFocusWithin)
{
System.Diagnostics.Debug.WriteLine(LostFocuse);
}
}


I have very simple scenario here. Look at layout, please:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <TextBox Grid.Row="0"></TextBox>
    <DatePicker Grid.Row="1" 
                Name="_datePicker"
                LostFocus="_datePicker_OnLostFocus"></DatePicker>
</Grid>

and codebehind:

private void _datePicker_OnLostFocus(object sender, RoutedEventArgs e)
{
    Debug.WriteLine("LostFocuse");
}

So, trouble is when I pick up some date and click TextBox then, event LostFocus fires 7 (seven!) times. One when DatePickerreally lost focus when I ckicked on TextBox and remaining six times totally are not explainable for me.

How can I fix it? I need only one fireing of this event. Or may be I can use some other event? I tried LostKeyBoardFocus with the same result.

解决方案

LostFocus is a routed event with route strategy set to Bubble. By bubble it means it will bubble upto its parent till root window until handled somewhere by explicitly setting e.Handled = true;.

So, that means even when child control loose focus it will bubble up to your datePicker that's why you see multiple hits to your method.

You can check for property IsKeyboardFocusWithin which returns if focus is within your control. Since you are not interested in listening to child lost focus event, you can check for this property in your handler like this and execute your code only when actual focus is lost by datePicker:

private void _datePicker_OnLostFocus(object sender, RoutedEventArgs e)
{
    DatePicker picker = sender as DatePicker;
    if (!picker.IsKeyboardFocusWithin)
    {
        System.Diagnostics.Debug.WriteLine("LostFocuse");
    }
}

这篇关于WPF DatePicker LostFocus触发7次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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