WPF DatePicker LostFocus触发7次 [英] WPF DatePicker LostFocus fires seven times
问题描述
< 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);
}
所以,麻烦的是当我拿起一些日期点击 我该如何解决?我只需要一个触发此事件。还是可以使用一些其他的事件?我尝试了 LostFocus是一个路由事件,路由策略设置为 所以,这意味着即使 ,当孩子控制松动的焦点时,它会浮起来你的datePicker,这就是为什么你看到多个点击你的方法 。 您可以检查属性 I have very simple scenario here. Look at layout, please: and codebehind: So, trouble is when I pick up some date and click How can I fix it? I need only one fireing of this event. Or may be I can use some other event? I tried LostFocus is a routed event with route strategy set to 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
这篇关于WPF DatePicker LostFocus触发7次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! TextBox
然后,事件 LostFocus
触发7(七!)次。当 TextBox
,而剩下的六次完全不能为我解释。 p>
LostKeyBoardFocus
,结果相同。 Bubble
。通过泡泡,它意味着它会冒泡到它的父母直到根窗口,直到通过明确设置 e.Handled = true;
来处理。
IsKeyboardFocusWithin
,如果焦点在您的控制。由于您对收听失落焦点事件不感兴趣,您可以在此处理程序中检查此属性,并且仅在datePicker丢失实际焦点时执行代码:
private void _datePicker_OnLostFocus(object sender,RoutedEventArgs e)
{
DatePicker选择器=发件人为DatePicker;
if(!picker.IsKeyboardFocusWithin)
{
System.Diagnostics.Debug.WriteLine(LostFocuse);
}
}
<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>
private void _datePicker_OnLostFocus(object sender, RoutedEventArgs e)
{
Debug.WriteLine("LostFocuse");
}
TextBox
then, event LostFocus
fires 7 (seven!) times. One when DatePicker
really lost focus when I ckicked on TextBox
and remaining six times totally are not explainable for me.LostKeyBoardFocus
with the same result. Bubble
. By bubble it means it will bubble upto its parent till root window until handled somewhere by explicitly setting e.Handled = true;
.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");
}
}