WPF中的AutoComplete TextBox绑定IEnumerable <String>

时间:2020-01-23 10:21:51

标签: c# wpf textbox ienumerable

我在https://github.com/Nimgoble/WPFTextBoxAutoComplete/找到了一个图书馆

但是我停留在步骤3

创建一个文本框,并将“ AutoCompleteItemsSource”绑定到IEnumerable的集合

我在https://docs.microsoft.com/en-us/dotnet/api/system.collections.ienumerable?view=netframework-4.8尝试了本教程

但是它仍然不起作用,我不知道问题出在哪里

这是我的代码

https://github.com/BudinMilk/textbox_autocomplete/tree/master/textbox_autocomplete

谢谢!

1 个答案:

答案 0 :(得分:0)

我遍历了您的代码,尝试了这些更改,我认为它将对您有用。

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.DataContext = this;
            InitializeComponent();
            TestItems = new ObservableCollection<string>()
            {
                "Apple",
                "Banana",
                "Carrot",
                "Dog",
                "Elderberry",
                "Fruit",
                "Grapes",
                "Honey",
                "Iron"
            };
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {   
            Person[] peopleArray = new Person[3]
            {
                new Person("John"),
                new Person("Jim"),
                new Person("Sue"),
            };

            People peopleList = new People(peopleArray);
            foreach (Person p in peopleList)
                Console.WriteLine(p.Name);
            Console.ReadLine();
        }

    private ObservableCollection<string> testItems ;
    public ObservableCollection<string> TestItems 
    {
        get { return testItems; }
        set
        {
            testItems= value;
            OnPropertyChanged("TestItems");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler == null) return;
        handler(this, new PropertyChangedEventArgs(name));
    }
 }
相关问题