I'm trying to access selected item from the wpf toolkit autoCompleBox (the one from codeplex) but i keep getting the null reference error.
I have really hit a brick wall with this!
here is my XAML:
<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"
<my:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ProductID}" FontWeight="Bold" Foreground="Black" Width="25"/>
<TextBlock Text="{Binding ProductBrandCode}" FontWeight="Bold" Foreground="Black" Width="55"/>
<TextBlock Text="{Binding ProductCode}" Foreground="Black"/>
</StackPanel>
</DataTemplate>
</my:AutoCompleteBox.ItemTemplate>
</my:AutoCompleteBox>
That's how i update the item source:
private void AutoBoxPopulateProductCode(object sender, System.Windows.Controls.PopulatingEventArgs e)
{
// TextBox t = (TextBox)sender;
autoTxtBoxProductCode.ItemsSource = Product.GetListOfProductCodesAndBrands(autoTxtBoxProductCode.Text.ToString());
autoTxtBoxProductCode.PopulateComplete();
}
What i'm trying to archive is when user hit enter I want to get the ProductID.
I have tried something like this:
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());
}
}
but the above returns null reference error.
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.
Help would be much appreciated.
EDIT:
Just after posting this question I found the solution. All I needed to add was:
SelectedItem="{Binding ElementName=this,
Path=ProductID,
Mode=TwoWay,
UpdateSourceTrigger=LostFocus}"
I hope it can help someone else with a similar problem.
Cheers,
Just after posting this question I found the solution. All I needed to add was:
SelectedItem="{Binding ElementName=this,
Path=ProductID,
Mode=TwoWay,
UpdateSourceTrigger=LostFocus}"