1

I've got a class like this:

class MyClass
{
    public int ID{ get; set; }
    public string Title{ get; set; }
}

Currently I'm binding it to AutoCompleteBox like this:

List<MyClass> lstMyClass = new List<MyClass>();
lstMyClass = context.Sometable;
autoCompleteBox1.ItemsSource = lstMyClass;
autoCompleteBox1.ValueMemberPath = "Title";
autoCompleteBox1.PopulateComplete();

It finds the objects by the title, but in autocomplete part it shows the class definition instead of the items title. Any idea?

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
Mehdi Ghanavatian
  • 37
  • 1
  • 2
  • 10
  • You are creating a new list, then throwing it away by overwriting it with `context.Sometable`. You can just assign the value of `ItemsSource` with `context.Sometable` directly. – Drew Noakes Mar 17 '13 at 20:14
  • @DrewNoakes this piece of code is just a sample im doing exactly what ur sayin, anyway the problem is the **`autocomplete`** part as i've mentioned, not the lists content. – Mehdi Ghanavatian Mar 17 '13 at 20:16
  • Check the API for other *MemberPath properties. I'm not familiar with it myself. – Drew Noakes Mar 17 '13 at 20:18
  • @DrewNoakes already done it, theres nothing like `DisplayMemberPath`. – Mehdi Ghanavatian Mar 17 '13 at 20:19

2 Answers2

3

You can use ItemTemplate (msdn).

<controls:AutoCompleteBox x:Name="autoCompleteBox1"        
      FilterMode="Contains"              
      IsTextCompletionEnabled="True">
    <controls:AutoCompleteBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Title}" />
        </DataTemplate>
    </controls:AutoCompleteBox.ItemTemplate>
</controls:AutoCompleteBox>
kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
  • It's great, tnx, can I add ItemTemplate programmaticlay? don't want any xaml code. if its possible it would be fantastic. – Mehdi Ghanavatian Mar 17 '13 at 20:55
  • @MehdiGhanavatian you should define your GUI in XAML, not in code-behind. But if you want to do that, check out this question: http://stackoverflow.com/questions/5471405/create-datatemplate-in-code-behind – kmatyaszek Mar 17 '13 at 21:09
  • I define GUI in xaml, i want to be able to customize everything. thats why i wanna add details in code-behind. – Mehdi Ghanavatian Mar 17 '13 at 21:39
1

You can override ToString() method in MyClass , so that it will return Title.

alex
  • 12,464
  • 3
  • 46
  • 67