views:

332

answers:

1

I'm having trouble getting the autocomplete box in System.Windows.Controls.Input working as I wish. When I start typing the dropdown section that displays the filtered list doesn't show the property that I'm binding to, it shows the class name instead.

So in the example below, when I type in my - instead of showing 'My Name' it shows MyNamespace.Person. However, when I select the item from the autocomplete list, it displays the FullName property in the textbox. I'm sure I'm just missing a simple autocomplete box property somewhere but I can't see it.

Example code:

public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName
        {
            get { return string.Format("{0} {1}", FirstName, LastName); }
        }
    }

In my xaml code behind I create some Person objects and store them in a list and bind that list to an autocomplete box

List<Person> people = new List<Person>();
people.Add(new Person { FirstName = "My", LastName = "Name" });
people.Add(new Person { FirstName = "Fernando", LastName = "Torres" });
acbNames.ItemsSource = people;

My xaml:

<my:AutoCompleteBox Name="acbNames" ValueMemberPath="FullName" />

/* after entering 'my', auto complete displays 'MyNamespace.Person' instead of 'My Name', but displays 'My Name' after selecting the item from the list */

A: 

It turns out I need to use an ItemTemplate for the dropdown part of the AutoCompleteBox, so the xaml for it would now be as follows:

<my:AutoCompleteBox Name="acbNames" ValueMemberBinding="{Binding FullName}">
            <my:AutoCompleteBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding FullName}"/>
                </DataTemplate>
            </my:AutoCompleteBox.ItemTemplate>
        </my:AutoCompleteBox>
Ciaran
I've also found out that if I provided my own implementation of ToString() or used a TypeConverter I can also get around this.
Ciaran