C# ComboBox GotFocus

后端 未结 3 1838
-上瘾入骨i
-上瘾入骨i 2021-02-05 17:21

I have a C# ComboBox using WPF. I have code that executes when the ComboBox\'s GotFocus is activated. The issue is that the GotFoc

相关标签:
3条回答
  • 2021-02-05 17:40

    I'm not too hot on WPF; but if you're trying to detect changes to the list (click on new value etc) you can use SelectedIndexChanged events..

    On the other hand, if you really do want to know simply when the control is focussed, can you filter it by saying something like;

    if (combo1.Focused && combo1.SelectedIndex == -1)
    {
         ...
    }
    

    .. ? It really depends on what youre trying to detect, exactly.

    0 讨论(0)
  • 2021-02-05 17:40

    Another solution is used is to determine whether the new focused element is an existing item in the combobox. If true then the LostFocus event should not be performed, because the combobox still has focus. Otherwise an element outside the combobox received focus.

    In the code snipplet below I added the functionality in a custom combobox class

    public class MyComboBox : System.Windows.Controls.Combobox
    {
        protected override void OnLostFocus(RoutedEventArgs e)
        {
            //Get the new focused element and in case this is not an existing item of the current combobox then perform a lost focus command.
            //Otherwise the drop down items have been opened and is still focused on the current combobox
            var focusedElement = FocusManager.GetFocusedElement(FocusManager.GetFocusScope(this));
            if (!(focusedElement is ComboBoxItem && ItemsControl.ItemsControlFromItemContainer(focusedElement as ComboBoxItem) == this))
            {
                base.OnLostFocus(e);
                /* Your code here... */
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-05 17:58

    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
    }
    

    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);
        }
    }
    

    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).

    To filter returned focus after selecting item, you can try to use DropDownOpened/DropDownClosed events with some field-flag.

    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.

    0 讨论(0)
提交回复
热议问题