这里's a way of doing it, although it might not cover all the cases - but you didn' t提供了太多的细节(例如,当已经选择了一个元素时会发生什么?你还想选择列表中的第一个元素吗?下面的代码只有当没有选择时才会突出显示第一个元素组合框 . 为了使它始终选择第一个元素,也应该处理 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();
}
}
2 回答
这里's a way of doing it, although it might not cover all the cases - but you didn' t提供了太多的细节(例如,当已经选择了一个元素时会发生什么?你还想选择列表中的第一个元素吗?下面的代码只有当没有选择时才会突出显示第一个元素组合框 . 为了使它始终选择第一个元素,也应该处理
DropDownOpened
事件 .(希望我理解正确,这就是你想要做的) .
它可能不是您正在寻找的但是如果您设置mycombo.SelectedIndex = 0然后mycombo.IsDropDownOpen = True它应该打开它并选择第一个项目 . 它将突出显示,但也将是组合框中的值 . 虽然我不确定这不是预期的效果 .