开发者

WPF Auto search combobox item

In WPF, when I type some text in combobox, it will highlight the item starting with the text I entered. This highlighting will search the whole text I have entered in the combo box. But instead I want the text search to match only the first letter. How to do this?

Sample Code to re-create the problem:

XAML:

<Window x:Class="ComboTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
  <StackPanel>
    <ComboBox x:Name="cb"></ComboBox>
  </StackPanel>
</Window>

Code Behind:

 namespace ComboTest
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            开发者_如何学编程InitializeComponent();
            cb.Items.Add("a");
            cb.Items.Add("ab");
            cb.Items.Add("abc");
            cb.Items.Add("agsf");
            cb.Items.Add("b1");
            cb.Items.Add("b2");
            cb.Items.Add("b3");
            cb.Items.Add("b4");
            cb.Items.Add("bbb");
            cb.Items.Add("bbbbb");
            cb.Items.Add("c4");
            cb.Items.Add("c");
            cb.Items.Add("c1");
            cb.Items.Add("c2");
            cb.Items.Add("cbb");
            cb.Items.Add("cbd");
            cb.Items.Add("d");
            cb.Items.Add("de");
            cb.Items.Add("df");

        }
    }
}

Now, run the solution, click on the combobox to open the pop up and press a continuously it will circle through all the items starting with a. I want the similar behavior when we press 'B' continiously.


There's an excellent article by Ioan Lazarciuc at http://www.lazarciuc.ro/ioan/2008/06/01/auto-complete-for-textboxes-in-wpf/ which you could use to simulate an auto-completing combo-box.

In your particular case, you'd need to change the private void Suggest() method to search the first letter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜