C# WPF AutoCompleteBox: How to bind text programmatically

时间:2016-04-04 17:32:23

标签: c# wpf xaml autocompletebox

From XAML I am able to create and give values to the properties of AutoCompleteBox but I want to do it programmatically.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace comboboxSuggestions
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var mylist = new List<person>
                             {
                                 new person()
                                 {
                                     name = "vikas",
                                     lastName = "bansal"
                                 },
                                  new person()
                                 {
                                     name = "viksit",
                                     lastName = "bansal"
                                 },
                                  new person()
                                 {
                                     name = "sunil",
                                     lastName = "bansal"
                                 }
                             };

            autocompleteBox.ItemsSource = mylist;
            autocompleteBox.ValueMemberPath = "name";
            var binding = new System.Windows.Data.Binding("name")
            {
            };
            autocompleteBox.SetBinding(AutoCompleteBox.TextProperty, binding);
        }

        private void autocompleteBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (autocompleteBox.SelectedItem != null)
            {
                person s = autocompleteBox.SelectedItem as person;

                string lastName = s.lastName;
            }
        }
    }

    public class person
    {
        public string name { get; set; }
        public string lastName { get; set; }
    }


}

XAML File

   <Controls:AutoCompleteBox Name="autocompleteBox" SelectionChanged="autocompleteBox_SelectionChanged"
                                      Height="30" Width="200" 
                                      />

Result

enter image description here

1 个答案:

答案 0 :(得分:0)

The problem is that your ItemsSource is a List<person>, but you haven't provided an ItemTemplate that tells the AutoCompleteBox what to display for each person. When you see "comboboxSuggestions.person", that's because the default ItemTemplate is just doing person.ToString().

Here's how you would resolve this:

First, add a fullname property to person, like this:

public class person
{
    public string name { get; set; }
    public string lastname { get; set; }

    public string fullname => name + " " + lastname;
}

Then, define an ItemTemplate within your XAML, like this:

<Controls:AutoCompleteBox Name="autocompleteBox"
                          SelectionChanged="autocompleteBox_SelectionChanged"
                          Height="30"
                          Width="200">
    <Controls:AutoCompleteBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding fullname}" />
        </DataTemplate>
    <Controls:AutoCompleteBox.ItemTemplate>
</Controls:AutoCompleteBox>

(Apologies if there are any typos, I haven't tested this code.)


Edit

To avoid defining a template in XAML, you could just override ToString() for person instead.

public class person
{
    public string name { get; set; }
    public string lastname { get; set; }

    public override string ToString()
    {
        return name + " " + lastname;
    }
}