禁用 WPF ComboBox 的下拉菜单

kma*_*ks2 3 .net c# wpf xaml combobox

我有一个如下指定的组合框:

<ComboBox Height="31" Margin="7,7,0,0" Name="callerID" IsEditable="True"  Background="LightBlue" KeyDown="callerIDbar_KeyDown" Foreground="White" FontSize="17"  FontWeight="Bold" HorizontalContentAlignment="Center" VerticalAlignment="Top" Grid.Row="0" Grid.Column="0"  />
Run Code Online (Sandbox Code Playgroud)

storedCalls是电话号码的集合,将填充到ComboBox.Items

foreach (string call in storedCalls)
{
   if (call != "Enter a number to dial")
      callerID.Items.Add(call);
}
Run Code Online (Sandbox Code Playgroud)

这一切都运行良好。我填充主项是因为我喜欢由 的集合Items中的值驱动的自动完成功能。有没有办法 XAML 禁用下拉错误并禁用下拉菜单?即制作一个简单的自动完成文本框之类的控件?ComboBoxItems

我已经看到了完整的TextBox控件,其中包括一堆代码隐藏和复杂的标记,但这不是我想要做的。我只需要禁用下拉菜单的显示功能。

小智 5

您可以处理该DropDownOpened事件然后将其关闭。
所以在 XAML 中你会得到:

<ComboBox x:Name="cb" DropDownOpened="cb_DropDownOpened"/>
Run Code Online (Sandbox Code Playgroud)

在代码隐藏中:

private void cbCategoria_DropDownOpened(object sender, EventArgs e)
    {
        ComboBox cb = sender as ComboBox;
        cb.IsDropDownOpen = false;
    }
Run Code Online (Sandbox Code Playgroud)

我更喜欢这个解决方案,而不是设置MaxDropDownHeight为 0。您的选择。