The Wpf combo box allows editing, and this is fine if all your combo box items are strings, or have a ToString() method defined on them.
When you select an item, it is displayed as Text, it does not use a DataTemplate, it just calls ToString() on the item that is selected.
I get a list of items in my combo drop down that are formatted using my item template, when i select one i get the name of the object i.e. MyNamespace.MyObjectName
Some solutions have been
is there another way around?
You can do this entirely within Xaml
<ComboBox IsTextSearchEnabled="True" IsEditable="True"
ItemsSource="{Binding MyObjectCollection}"
TextSearch.TextPath="MyObjectName">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding MyObjectName}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The upside is that you can define and change this however you want in your XAML without any code-behind. You bind the ItemsSource to your collection of objects, and then you set the path on which to base your search to TextSearch.TextPath. Then, within you custom ItemTemplate you can bind the TextBlock to something else outside of the object's ToString method.
You can use an IValueConverter to convert the "object" to a string value and back. See the example code in the IValueConverter link for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With