简体   繁体   中英

How to bind a class to WPF AutoCompleteBox?

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?

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>

您可以在MyClass中覆盖ToString()方法,以便它返回Title。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM