title: C# WPF AutoCompleteBox: How to bind text programmatically
body: <p>From XAML I am able to create and give values to the properties of AutoCompleteBox but I want to do it programmatically.</p>
  
  <pre><code>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
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
          }
  
          private void Window_Loaded(object sender, RoutedEventArgs e)
          {
              var mylist = new List&lt;person&gt;
                               {
                                   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; }
      }
  
  
  }
  </code></pre>
  
  <p><strong>XAML File</strong></p>
  
  <pre><code>   &lt;Controls:AutoCompleteBox Name="autocompleteBox" SelectionChanged="autocompleteBox_SelectionChanged"
                                        Height="30" Width="200" 
                                        /&gt;
  </code></pre>
  
  <p><strong>Result</strong></p>
  
  <p><a href="https://i.stack.imgur.com/aXhLT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aXhLT.png" alt="enter image description here"></a></p>