在WPF中自动完成Combobox [英] Autocomplete Combobox in WPF

查看:261
本文介绍了在WPF中自动完成Combobox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试自动完成一个文本框后,盖伊试图自动完成组合框。因为我无法将所有控件添加到文本框中。但是在组合框中,我做到了。但是问题也就在那里。当我输入组合框时它是弹出窗口很好地根据键入的文本过滤项目,但我需要通过鼠标选择一个选项,不能通过键盘箭头。我试图解决它与keydown事件但不知何故它不能集中组合框我认为。有人可以建议我在这里有什么不对我我在做什么或如何启用键盘arrowkey事件?以下是我的代码:



XML:

 <   ComboBox    高度  =  23    Horizo​​ntalAlignment   =    保证金  =   160,236,0,0   名称  =  comboBox1   < span class =code-attribute> VerticalAlignment   =  Top   宽度  =  120    IsEditable   =  True    ItemsSource   =  {Binding}    KeyDown   =  comboBox1_KeyDown >  
< ComboBox。资源 >
< sys:Double x:Key = {x:Static SystemParameters.VerticalScrollBarWidthKey} > 0 < / sys:Double >
< span class =code-keyword>< / ComboBox.Resources >
< / ComboBox >





C#:

公共部分类MainWindow:Window 
{
List< string>名单;

public MainWindow()
{
InitializeComponent();
nameList = new List< string>
{
A0-Word,B0-Word,C0-Word,
A1-Word,A111,A11122,B1-Word ,C1-Word,
B2-Word,C2-Word,
C3-Word,
};

comboBox1.DataContext = nameList;

comboBox1.Loaded + =委托
{
System.Windows.Controls.TextBox textBox = comboBox1.Template.FindName(PART_EditableTextBox,comboBox1)as System.Windows.Controls 。文本框;
Popup popup = comboBox1.Template.FindName(PART_Popup,comboBox1)为Popup;
if(textBox!= null)
{
textBox.TextChanged + = delegate
{
comboBox1.Items.Filter + =(item)=>
{
if(item.ToString()。StartsWith(textBox.Text))
{
popup.IsOpen = true;
返回true;

}
else
{
// popup.IsOpen = false;
返回false;
}
};

};
}
};

comboBox1.KeyDown + = new System.Windows.Input.KeyEventHandler(comboBox1_KeyDown);

}

private void comboBox1_KeyDown(object sender,System.Windows.Input.KeyEventArgs e)
{

if(e。 Key == Key.Down)
{
e.Handled = true;
comboBox1.Items.MoveCurrentToNext();
}
}

解决方案

仅供参考,WPF Toolkit()中有一个很好的自动完成文本框。为什么不使用它?


Guy's after trying to autocomplete a textbox i move towards now to an autocomplete combobox.Because i cannot add all controls to a textbox.But in combobox,i did that.But a problem also stands there.When i type in combobox it's popup filters the items according to typed text nicely,but i need to select a option by mouse,cannot by keyboard arrow's.I tried to solve it with keydown event but somehow it cannot focus combobox i think.Someone can suggest me here what wrong i am doing or how can i enable keyboard arrowkey events?Below are my codes:

XML:

<ComboBox Height="23" HorizontalAlignment="Left" Margin="160,236,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" IsEditable="True" ItemsSource="{Binding}" KeyDown="comboBox1_KeyDown">
    <ComboBox.Resources>
        <sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">0</sys:Double>
    </ComboBox.Resources>
</ComboBox>



C#:

    public partial class MainWindow : Window
    {
        List<string> nameList;
        
        public MainWindow()
        {
            InitializeComponent();
            nameList = new List<string> 
            {
                "A0-Word","B0-Word","C0-Word",
                "A1-Word","A111","A11122","B1-Word","C1-Word",
                "B2-Word","C2-Word",
                "C3-Word",
            };

           comboBox1.DataContext = nameList;

            comboBox1.Loaded += delegate
            {
                System.Windows.Controls.TextBox textBox = comboBox1.Template.FindName("PART_EditableTextBox", comboBox1) as System.Windows.Controls.TextBox;
                Popup popup = comboBox1.Template.FindName("PART_Popup", comboBox1) as Popup;
                if (textBox != null)
                {
                    textBox.TextChanged += delegate
                    {
                            comboBox1.Items.Filter += (item) =>
                            {
                                if (item.ToString().StartsWith(textBox.Text))
                                {
                                    popup.IsOpen = true;
                                    return true;
                                    
                                }
                                else
                                {
                                   // popup.IsOpen = false;
                                    return false;
                                }
                            };

                    };
                }
            };

            comboBox1.KeyDown+=new System.Windows.Input.KeyEventHandler(comboBox1_KeyDown);
            
        }

private void comboBox1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{

    if (e.Key == Key.Down)
    {
        e.Handled = true;
        comboBox1.Items.MoveCurrentToNext();
    }
}

解决方案

FYI, there is nice auto-complete text box in WPF Toolkit (). Why don`t you use it?


这篇关于在WPF中自动完成Combobox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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