C#组合框的GotFocus [英] C# ComboBox GotFocus
问题描述
我使用WPF一个C#组合框
。我有当组合框
的的GotFocus
启动执行代码。问题是,的GotFocus
事件被执行的每个选择从组合框
制作的时间。例如,的GotFocus
当你第一次点击组合框
并执行,那么,当你做出选择,即使你没有点击任何其他控制。
I have a C# ComboBox
using WPF. I have code that executes when the ComboBox
's GotFocus
is activated. The issue is that the GotFocus
event is executed every time a selection is made from the ComboBox
. For example, the GotFocus
is executed when you first click on the ComboBox
and then when you make a selection even though you have not click on any other control.
是否有可能阻止这一事件的,如果选择在列表中正在取得或者是有标志或别的东西在烧可以用来确定是否在的GotFocus
的事件处理程序被解雇作为用户在列表中选择一个项目的结果的事件处理程序
Is it possible to prevent this event from firing if a selection is being made in the list or is there a flag or something else in the event handler that can be used to determine if the GotFocus
event handler was fired as a result of the user selecting an item in the list?
推荐答案
您可以解决这个问题,接下来的验证:
You can solve this problem with next verification:
private void myComboBox_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() == typeof(ComboBoxItem))
return;
//Your code here
}
这代码将过滤所有焦点事件从项目(因为他们使用泡沫路由事件)。但是还有另外一个问题 - WPF的组合框焦点特定行为:您的组合框失去焦点和物品获得的物品,当你打开的下拉列表中。当您选择了一些项目 - 项目失去焦点和ComboBox回来。下拉列表就像是另一个控制。您可以通过简单的代码中看到这一点:
This code will filter all focus events from items (because they use bubble routing event). But there is another problem - specific behaviour of WPF ComboBox focus: when you open drop-down list with items your ComboBox losing focus and items get. When you select some item - item losing focus and ComboBox get back. Drop-down list is like another control. You can see this by simple code:
private void myComboBox_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem))
{
Trace.WriteLine("Got " + DateTime.Now);
}
}
private void myComboBox_LostFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem))
{
Trace.WriteLine("Lost " + DateTime.Now);
}
}
所以你会得到无论如何ATLEAST两种对焦事件:当您选择组合框,当你在它选择的东西(焦点将返回组合框)。
So you will get anyway atleast two focus events: when you select ComboBox and when you selecting something in it (focus will return to ComboBox).
要在选择项目后,过滤返回焦点时,您可以尝试使用 DropDownOpened
/ DropDownClosed
事件与一些领域标志。
To filter returned focus after selecting item, you can try to use DropDownOpened
/DropDownClosed
events with some field-flag.
只有1获得焦点事件代码:
So the final code with only 1 event of getting focus:
private bool returnedFocus = false;
private void myComboBox_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem) && !returnedFocus)
{
//Your code.
}
}
private void myComboBox_LostFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem))
{
ComboBox cb = (ComboBox)sender;
returnedFocus = cb.IsDropDownOpen;
}
}
选择这个例子你实际需要更多的为您应用程序。
Choose from this examples what you actually need more for your application.
这篇关于C#组合框的GotFocus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!