禁用 WPF ComboBox 的下拉菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19663875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:58:39  来源:igfitidea点击:

Disable the drop down menu of a WPF ComboBox

c#.netwpfxamlcombobox

提问by kmarks2

I have a ComboBox as specified below:

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

<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"  />

storedCallsis a collection of phone numbers that will be populated to the ComboBox.Items:

storedCalls是将填充到以下内容的电话号码集合ComboBox.Items

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

All this works fine. I populate the Itemsprimary because I like the autocomplete that is driven by the values in the ComboBox's Itemscollections. Is there a way the XAML to disable the drop down error, and disable the drop down menu? I.e. make a simple auto complete textbox like control?

所有这些工作正常。我填充Items主要是因为我喜欢由ComboBox'sItems集合中的值驱动的自动完成。XAML 有没有办法禁用下拉错误并禁用下拉菜单?即制作一个简单的自动完成文本框之类的控件?

I have seen full on TextBoxcontrols that include a bunch of code-behind and complicated markup, and this is not what I am looking to do. I just need to disable the ability of the drop down menu from showing.

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

回答by Mr.Pidra

You can handle the DropDownOpenedevent and then close it.
So in n the XAML you get:

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

<ComboBox x:Name="cb" DropDownOpened="cb_DropDownOpened"/>

And in Code Behind:

在代码隐藏中:

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

I prefear this solution reather than set MaxDropDownHeightto 0. Your choice.

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