这是将AutoSuggest构建到WPF ComboBox的最佳方法吗? [英] Is this the best way to build AutoSuggest into a WPF ComboBox?

查看:167
本文介绍了这是将AutoSuggest构建到WPF ComboBox的最佳方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个国籍组合框,像下面的一个,想要使用户可以输入字母到中的选择。我可以通过在NationalityComboBox_KeyDown方法中添加逻辑来解决这个问题。

I have a Nationality ComboBox like the one below and want to make it so that the user can type letters to narrow in on the choices. I could solve this the way I started below by adding logic in the NationalityComboBox_KeyDown method.

这是构建AutoSuggest XAML:

Is this the best way to build AutoSuggest into a ComboBox or is there a built-in way to do the same thing?

XAML:

代码背后

public partial class NationalityComboBox : ComboBox
{
    public NationalityComboBox()
    {
        InitializeComponent();

        Items.Add(new KeyValuePair<string, string>(null, "Please choose..."));
        Items.Add(new KeyValuePair<string, string>(null, "American"));
        Items.Add(new KeyValuePair<string, string>(null, "Australian"));
        Items.Add(new KeyValuePair<string, string>(null, "Belgian"));
        Items.Add(new KeyValuePair<string, string>(null, "French"));
        Items.Add(new KeyValuePair<string, string>(null, "German"));
        Items.Add(new KeyValuePair<string, string>(null, "Georgian"));
        SelectedIndex = 0;

        KeyDown += new KeyEventHandler(NationalityComboBox_KeyDown);

    }

    void NationalityComboBox_KeyDown(object sender, KeyEventArgs e)
    {
        SelectedIndex = 4; // can create logic here to handle key presses, e.g. "G", "E", "O"....
    }
}


推荐答案

我写了一个附加的属性来做:

I wrote an attached property to do that :

public class ComboBoxAutoFilter
{
    public static bool GetEnabled(DependencyObject obj)
    {
        return (bool)obj.GetValue(EnabledProperty);
    }

    public static void SetEnabled(DependencyObject obj, bool value)
    {
        obj.SetValue(EnabledProperty, value);
    }

    // Using a DependencyProperty as the backing store for Enabled.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty EnabledProperty =
        DependencyProperty.RegisterAttached(
            "Enabled",
            typeof(bool),
            typeof(ComboBoxAutoFilter),
            new UIPropertyMetadata(false, Enabled_Changed)
        );

    private static void Enabled_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        ComboBox combo = sender as ComboBox;
        if (combo != null)
        {
            if (combo.Template != null)
                SetTextChangedHandler(combo);
            else
                combo.Loaded += new RoutedEventHandler(combo_Loaded);
        }
    }

    static void combo_Loaded(object sender, RoutedEventArgs e)
    {
        ComboBox combo = sender as ComboBox;
        combo.Loaded -= combo_Loaded;
        if (combo.Template != null)
            SetTextChangedHandler(combo);
    }

    private static void SetTextChangedHandler(ComboBox combo)
    {
        TextBox textBox = combo.Template.FindName("PART_EditableTextBox", combo) as TextBox;
        if (textBox != null)
        {
            bool enabled = GetEnabled(combo);
            if (enabled)
                textBox.TextChanged += textBox_TextChanged;
            else
                textBox.TextChanged -= textBox_TextChanged;
        }
    }

    private static void textBox_TextChanged(object sender, RoutedEventArgs e)
    {
        TextBox textBox = sender as TextBox;
        ComboBox combo = textBox.TemplatedParent as ComboBox;
        combo.IsDropDownOpen = true;
        string text = textBox.Text.Substring(0, textBox.SelectionStart);
        combo.Items.Filter = value => value.ToString().StartsWith(text);
    }

}

    <ComboBox IsEditable="True"
              local:ComboBoxAutoFilter.Enabled="True">
        <sys:String>American</sys:String>
        <sys:String>Australian</sys:String>
        <sys:String>Belgian</sys:String>
        <sys:String>French</sys:String>
        <sys:String>German</sys:String>
        <sys:String>Georgian</sys:String>
        ...
    </ComboBox>

这篇关于这是将AutoSuggest构建到WPF ComboBox的最佳方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆