title: C# Wpf toolkit autocomplebox - how to get the selected item?
body: <p>I'm trying to access selected item from the wpf toolkit autoCompleBox (the one from codeplex) but i keep getting the null reference error.</p>
  
  <p>I have really hit a brick wall with this!</p>
  
  <p>here is my XAML:</p>
  
  <pre><code>     &lt;my:AutoCompleteBox
       x:Name="autoTxtBoxProductCode"
       VerticalAlignment="Top" Height="28" Margin="112,10,0,0" Width="144"           
       Background="#FFEDF4AB"
       Populating="AutoBoxPopulateProductCode"
       ValueMemberPath="ProductCode"
       PreviewKeyUp="autoTxtBoxProductCode_PreviewKeyUp"
       HorizontalAlignment="Left" 
       IsTextCompletionEnabled="False"
  
        &lt;my:AutoCompleteBox.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                &lt;StackPanel Orientation="Horizontal"&gt;
                    &lt;TextBlock Text="{Binding ProductID}" FontWeight="Bold" Foreground="Black" Width="25"/&gt;
                    &lt;TextBlock Text="{Binding ProductBrandCode}" FontWeight="Bold" Foreground="Black" Width="55"/&gt;
                    &lt;TextBlock Text="{Binding ProductCode}" Foreground="Black"/&gt;
                  &lt;/StackPanel&gt;
                &lt;/DataTemplate&gt;         
               &lt;/my:AutoCompleteBox.ItemTemplate&gt;
          &lt;/my:AutoCompleteBox&gt;
  </code></pre>
  
  <p>That's how i update the item source:</p>
  
  <pre><code>    private void AutoBoxPopulateProductCode(object sender, System.Windows.Controls.PopulatingEventArgs e)
      {
          // TextBox t = (TextBox)sender;
  
          autoTxtBoxProductCode.ItemsSource = Product.GetListOfProductCodesAndBrands(autoTxtBoxProductCode.Text.ToString());
          autoTxtBoxProductCode.PopulateComplete();
      }
  </code></pre>
  
  <p>What i'm trying to archive is when user hit enter I want to get the ProductID.</p>
  
  <p>I have tried something like this:</p>
  
  <pre><code>   private void autoTxtBoxProductCode_PreviewKeyUp(object sender, KeyEventArgs e)
      {
  
          if (e.Key != Key.Enter)
          { return; }
          else
          {   
              Product prd = (Product)(autoTxtBoxProductCode.SelectedItem);
              MessageBox.Show(prd.ProductID.ToString());
  
          }
  
      }
  </code></pre>
  
  <p>but the above returns null reference error.</p>
  
  <p>I must be missing some sort of binding but since WPF is new to me I can't figure out what needs to be changed. </p>
  
  <p>Help would be much appreciated.</p>
  
  <p>EDIT:</p>
  
  <p>Just after posting this question I found the solution. All I needed to add was:</p>
  
  <pre><code>SelectedItem="{Binding ElementName=this,
                 Path=ProductID,
                 Mode=TwoWay,
                 UpdateSourceTrigger=LostFocus}"
  </code></pre>
  
  <p>I hope it can help someone else with a similar problem.</p>
  
  <p>Cheers,</p>