C # WPF - ComboBox

I am working on a custom control that internally uses a ComboBox.

My problem is that the ComboBox is focused and has a drop down list, it seems to focus the entire control. I would like to automatically select the first element in the stream, but now you need to press the Down key to do this.

Is there a way to programmatically highlight the first item in a ComboBox (set the readonly IsHighlighted property to true)? I believe that the IsHighlight concept inside the ComboBox is different from the Focus concept. Also, I am binding via ItemsSource, so I have no reference to ComboBoxItems.

+3
source share
2 answers

, - (, , ? ? , . , DropDownOpened).

public MainWindow()
{
    InitializeComponent();
    combobox.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);
}

void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
    if (combobox.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
    {
        (combobox.ItemContainerGenerator.ContainerFromIndex(0) as ComboBoxItem).Focus();
    }
}

(, , , ).

+3

, , , mycombo.SelectedIndex = 0, mycombo.IsDropDownOpen = True, . , . , , ..

0

Source: https://habr.com/ru/post/1783901/


All Articles