20

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

  • use ValuePath to bind to a property on the object, but if you require your display to be more than one of these, bad luck.
  • implement the ToString() method on your object

is there another way around?

Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228

2 Answers2

37

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.

g t
  • 7,287
  • 7
  • 50
  • 85
sidney.andrews
  • 5,146
  • 3
  • 23
  • 29
  • Forgot to add, the key here is to keep your DataTemplate. The TextSearch.TextPath attached property is what makes your textbox within the ComboBox show whatever property you want. – sidney.andrews Feb 19 '10 at 13:20
  • 1
    absolute gold. the attached properties get me every time, because its so easy to stay unaware of them. Thanks. – Aran Mulholland Feb 26 '10 at 01:29
  • *OMG* I wish I could upvote this more than once. I also wish it had come up when I searched for "WPF ComboBox SelectedValuePath ToString" -- hopefully mentioning them in a comment will help some other poor sap not chase his tail for 2 hours trying to figure this out in the future. Half the battle in using a large framework is learning how to think like the people that designed the framework (assuming you can't just memorize the entire huge framework. ;-)) I was definitely thinking at this one in the "wrong" direction. – Rick Riensche Aug 06 '12 at 20:17
  • 2
    What if you are using multiple bound properties inside that datatemplate. I tried it on mine and now when you select something from the drop down it only displays the path i set to TextSearch.TextPath. The items in the drop down display properly but the selected item display does just the the TextPath i used. – Cody W Hageman Jan 09 '20 at 20:24
3

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.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • thanks for the answer, man i feel like an idiot, i knew this. ill blame it on friday. :) – Aran Mulholland Dec 04 '09 at 04:50
  • ive used converters many times, where would you put this converter, not on the ItemsSource property, Ive tried on the SelectedItem property and the converter gets hit, but when i return a string i still get the MyNamespace.MyObjectName, have you tried this with a combobox before? – Aran Mulholland Dec 04 '09 at 05:05
  • Check out the example in the link. It shows you how to use a converter with a combobox (via the comboboxes ItemsTemplate). – Reed Copsey Dec 04 '09 at 16:50