How do I show the drop-down in a combobox in WPF?

2019-05-10 08:12发布

When the user starts typing in a combobox, auto-complete kicks in and shows the closest match. What I want to do is have the drop-down become visible as well, as if the user clicked the down arrow. Can this be done and if yes, how?

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-05-10 09:06

You could hook the KeyDown event on the ComboBox, and then set the IsDropDownOpen property to true.

in XAML:

<ComboBox x:Name="MyComboBox"
          IsEditable="True"
          IsReadOnly="False"
          KeyDown="MyComboBox_KeyDown"/>

in code behind:

private void MyComboBox_KeyDown(object sender, KeyEventArgs e) {
    if (MyComboBox.Text.Length > 0)
        MyComboBox.IsDropDownOpen = true;
}
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-05-10 09:16
ComboBox comboBox = new ComboBox;
comboBox.DroppedDown = true;
查看更多
登录 后发表回答