wpf - 在 wpf 的 Combobox 中关闭自动完成

标签 wpf silverlight xaml combobox

我正在使用 .NET Framework 4.0 构建我的应用程序。

我有一个组合框,我想在其中关闭组合框的建议附加模式。相反,我想要仅建议模式。

在许多用户要求关闭自动完成功能的问题中,我得到的都是相同的答案。即,将 IsTextSearchEnabled 设置为 False。

当 IsTextSearchEnabled = True 时

enter image description here

当 IsTextSearchEnabled = False 时

enter image description here

我想要的是:

enter image description here

当用户在组合框上按 Enter 时,我希望将项目附加到组合框的文本框。

这东西在WPF中可行吗?

最佳答案

就像这里 promise 的那样是演示。如您所见,我做了我在评论中解释的事情。我听了文本更改事件。

检查一下:

<Grid>
    <local:MyComboBox x:Name="comboBox" IsEditable="True"
              VerticalAlignment="Center"
              IsTextSearchEnabled="True">
        <ComboBoxItem>hello</ComboBoxItem>
        <ComboBoxItem>world</ComboBoxItem>
        <ComboBoxItem>123</ComboBoxItem>
    </local:MyComboBox>
</Grid>

public class MyComboBox : ComboBox
{
    private string myValue;
    private bool needsUpdate;

    public override void OnApplyTemplate()
    {
        TextBox tbx = this.GetTemplateChild("PART_EditableTextBox") as TextBox;

        tbx.PreviewKeyDown += (o, e) =>
        {
            this.needsUpdate = true;
        };

        tbx.TextChanged += (o, e) =>
            {
                if (needsUpdate)
                {
                    myValue = tbx.Text;
                    this.needsUpdate = false;
                }
                else
                {
                    tbx.Text = myValue;
                }
            };

        base.OnApplyTemplate();
    }
}

关于wpf - 在 wpf 的 Combobox 中关闭自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20291213/

相关文章:

c# - WPF Canvas 无法在 XP 上渲染以编程方式添加的子项

c# - ContextMenuItem isEnabled 绑定(bind)到 Object 属性

silverlight - Silverlight 中的 MVC

wpf - wpf 中包含静态和动态数据的组合框

wpf - 使用 WrapPanel 样式化 ListView.GroupStyle

c# - 数据网格单元格单击事件

c# - WPF UI Automation with .NET 4.5 with Prism 和 MVVM with Click Once App 便于非开发人员使用

silverlight - 使用转义字符串格式进行绑定(bind)会破坏 VS IDE 和智能感知

c# - 从独立存储加载大量图像时出现 OutOfMemory 异常

c# - 为标签添加右边框