开发者

How to Bind an Observable collection in Autocomplete Text box?

开发者 https://www.devze.com 2023-04-09 01:56 出处:网络
I need to bind an Observable collection to my AutoComplete Text box in the WPF application. But When I run the application after binding in the designer, I got a display with my full BL class Referenc

I need to bind an Observable collection to my AutoComplete Text box in the WPF application. But When I run the application after binding in the designer, I got a display with my full BL class Reference in the dropdown开发者_如何学Python. How to avoid this behaviour? Also I want to show some other properties in the dropdown(as details view) and I should be able to get the Other properties of the object once I select a item from the List. Is anybody can come with a code snippet?


There is ItemTemplate property in the AutoCompleteTextBox. You can use that to display whatever you want in the drop down list.


Without seeing your code, I'm assuming your ObservableCollection is a user defined object and you're binding the ItemsSource to the ObservableCollection and not providing a DataTemplate. The controls within the DataTemplate would then be bound to the public properties of the object stored in the ObservableCollection.

The DataTemplate would also allow for you to show all the properties you want in the dropdown listbox. Since I'm unaware of the auto-complete textbox you are using I can't really give an example of the DataTemplate.

Here is a simple example of a ListBox DataTemplate:

<ListBox Width="400" Margin="10"
     ItemsSource="{Binding Source={StaticResource myTodoList}}">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <StackPanel>
         <TextBlock Text="{Binding Path=TaskName}" />
         <TextBlock Text="{Binding Path=Description}"/>
         <TextBlock Text="{Binding Path=Priority}"/>
       </StackPanel>
     </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

Here is a link which will give you all the info you need: http://msdn.microsoft.com/en-us/library/ms742521.aspx.

0

精彩评论

暂无评论...
验证码 换一张
取 消