Keren
Keren

Reputation: 81

WPF Combobox display entity name in dropdown instead of DisplayMemberPath

I have the following combobox:

<ComboBox Name="cbBonusType" 
          DisplayMemberPath="BonusTypeName"  
          SelectedValuePath="ID" Width="150" Margin="10,0,0,0"                  
          SelectionChanged="cbBonusType_SelectionChanged"/>

When running: When selecting an item, the combobox shows exactly the right string. But while the droppbox is open, the named displayed in the droppbox are all set to the name of the entity: "CaSaMa.WPF.UI.Competiotion.BonusType".

Why is that and how do I fix it?

Upvotes: 1

Views: 4270

Answers (2)

MattE
MattE

Reputation: 1114

Its way easier than this...in XAML:

<ComboBoxItem Content="This Value" Tag="This Value"/>

Then in code behind:

GetValue=ComboBoxName.selecteditem.tag.tostring()

GetValue will be "This Value" instead of "System.Windows.Controls.ComboBoxItem: This Value"

Upvotes: 0

Steoates
Steoates

Reputation: 3168

Best bet would be override the ToString on your object - that would give you a consistent display across your application.

hope that helps!

Just re-read your question - think I got the wrong end of the stick.. What you can do is set the ItemTemplate on the control instead like this;

      <ComboBox Name="cbBonusType" 
      ItemTemplate="{StaticResource DisplayTemplate}"
      SelectedValuePath="ID" Width="150" Margin="10,0,0,0"                  
      SelectionChanged="cbBonusType_SelectionChanged"/>

then create the template like this

    <DataTemplate x:Key="DisplayTemplate"
          DataType="{x:Type <YOURTYPE>}">
     <TextBlock Text="{Binding BonusTypeName}"/>
    </DataTemplate>

its a known problem and this is the workaround sadly!

hope it helps.. this time! :)

edit : updated code just incase anyone else would like to use it!

ste.

Upvotes: 6

Related Questions