Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoSuggestBox shows up Property name instead of value

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

enter image description here

When I click an Item in this list

enter image description here

Again I get the selected Item in the suggestions box. When I click it I get the property Name instead of the value

enter image description here

like image 549
Ankit Keer Avatar asked Dec 05 '22 02:12

Ankit Keer


1 Answers

Use

TextMemberPath="entityName"

instead of

TextMemberPath="{Binding entityName}"
like image 192
hasi05 Avatar answered Dec 22 '22 07:12

hasi05