0
votes

I have encountered the following problem:

ISSUE: ComboBoxItems displaying perfectly but displaying whole object when items are selected.

1. Clicking on comboBox: ComboBoxItems displaying perfectly

enter image description here

2. One ComboBoxitem is selected: Whole Entity Framework object is displayed instead of just item name.

enter image description here

Here is an example of what is displayed when a ComboBoxItem is selected: System.Data.Entity.DynamicProxies.tblContainer_C0BE4F13C798ED380A1E249BFB338D265E97D4F4C7A880C5D813102738561025

Desired result: Bout verre bouchon emery

This is a minified version of my code:

<ComboBox IsEditable = "True" ItemsSource="{Binding ProductsContainer}" SelectedValuePath="ContainerID">
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock 
                                        FontSize="15"
                                        Width="100"
                                        HorizontalAlignment="Center"
                                        Text="{Binding ContainerName}" 
                                        TextWrapping="Wrap" />
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
</ComboBox>

Here is the list to which the ComboBox binds:

public virtual List<tblContainer> ProductsContainer { get { return db.tblContainer.ToList(); } }

ProductsContainer has the attributes: ContainerID and ContainerName

1
add the code of the class tblContainer or at least the minimal code needed for your example to work.Blechdose
You probably don't want to set IsEditable to true. The ItemTemplate is not applied then. See e.g. here: stackoverflow.com/q/27636357/1136211Clemens

1 Answers

1
votes

Seems to work when IsTextSearchEnabled="True"and TextSearch.TextPath="ContainerName" is used.

<ComboBox IsEditable="True" ItemsSource="{Binding ProductsContainer}" SelectedValuePath="ContainerID" 
          IsTextSearchEnabled="True" TextSearch.TextPath="ContainerName">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock 
                    FontSize="15"
                    Width="100"
                    HorizontalAlignment="Center"
                    Text="{Binding ContainerName}" 
                    TextWrapping="Wrap" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>