On selection of item in AutoSuggestBox instead of the property value it binds to the property.
This is my xaml.
<AutoSuggestBox x:Name="txtSearchBox" ItemsSource="{Binding}"
PlaceholderText="Search in Distributor" Style="{StaticResource AutoSuggestBoxStyle1}"
Margin="10,25,10,0" DisplayMemberPath="{Binding entityName}" TextMemberPath="{Binding entityName}"
BorderBrush="#000000" BorderThickness="2" TextChanged="txtSearchBox_TextChanged"
SuggestionChosen="txtSearchBox_SuggestionChosen">
<AutoSuggestBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding entityName}"
Tag="{Binding entityId}"/>
</DataTemplate>
</AutoSuggestBox.ItemTemplate>
</AutoSuggestBox>
This is the Code Behind
private void txtSearchBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
List<Master_User_Hierarchy_VM> lstUserHierarchy = new List<Master_User_Hierarchy_VM>();
txtSearchBox.ItemsSource = null;
foreach (Master_User_Hierarchy_VM obj in lstMaster_UserHierarchy_VM)
{
if (sender.Text != "")
{
if (obj.entityName.Contains(sender.Text))
{
lstUserHierarchy.Add(obj);
}
}
}
txtSearchBox.ItemsSource = lstUserHierarchy;
}
else if (args.Reason == AutoSuggestionBoxTextChangeReason.SuggestionChosen)
{
//txtSearchBox.Text = txtSearchBox.Items[0].ToString();
}
}
private void txtSearchBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
{
txtSearchBox.Text = ((Master_User_Hierarchy_VM)args.SelectedItem).entityName;
}
This is when I enter a character
When I click an Item in this list
Again I get the selected Item in the suggestions box. When I click it I get the property Name instead of the value
Use
TextMemberPath="entityName"
instead of
TextMemberPath="{Binding entityName}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With