开发者

Strange issues with drop down list of Combobox

Once again I can't find a neat solution for a simple UI problem in WPF. I want the combo box drop down list to appear whenever combo box gets focus. So I wrote this in the got focus event:

 private void comboBoxAC_Cat_GotFocus(object sender, RoutedEventArgs e)
    {
        comboBoxA开发者_Python百科C_Cat.IsDropDownOpen = true;
    }

But now the problem is that once the drop down list opens, the application is kind of stuck in it. It can not come out of the drop down list no matter what I do, whether I press enter or anything. I tried registering to lost focus or other events but nothing seems to work. Here is a list of my event handlers in the application which become useless once I get inside the drop down list.

private void Grid_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        var uie = e.OriginalSource as UIElement;
        if (e.Source is Button)
            return;
        if (e.Key == Key.Enter)
        {
            e.Handled = true;
            uie.MoveFocus(
            new TraversalRequrest(
            FocusNavigationDiection.Next));
        }
    }
    private void comboBoxAC_Cat_LostFocus(object sender, RoutedEventArgs e)
    {
        (sender as ComboBox).IsDropDownOpen = false;

    }

Can any one help me out with this please?

My Basic requirement is simple: Drop Down list should open as soon as Combobox is focused using tab or mouse. Then the user should be able to select the items and once he presses enter selecting a item from drop down list, it should close and focus should move to next ui element.

Now is it hard to achieve??? I thought that was exactly the functionality of a combobox


I don't understand, If I use exactly this code below

    private bool returnedFocus = false;

    private void myComboBox_GotFocus(object sender, RoutedEventArgs e)
    {
        if (e.OriginalSource.GetType() != typeof(ComboBoxItem) && !returnedFocus)
        {
            cmb.IsDropDownOpen = true;
        }
    }

    private void myComboBox_LostFocus(object sender, RoutedEventArgs e)
    {
        if (e.OriginalSource.GetType() != typeof(ComboBoxItem))
        {
            ComboBox cb = (ComboBox)sender;
            returnedFocus = cb.IsDropDownOpen;
        }
    }

I get exactly what i think you want, my combobox drop down opens when control get focus and if i choose a listitem pressing enter or through mouse click contol loses focus

Isn't that what you wanted?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜