views:

54

answers:

3

Hi Im a begginer in wpf. Actualy im styling a Combobox which receive data from a database. The combobox work fine at this point (without style). I have edited the Control template of the ComboboxItem control in stylesheet, lazing it like resource dictionary. If the style apply to combobox with static data, the style work fine, but if the style apply to combobox with dinamic data (binding form database in this case) the items list only returns the object( the stirns showed is similar like "Class.Method.Property") but not the content of property that´s i need to show. I have been trying all, and i read everything on the internet about xaml styles and combobox template but i couldn´t solve the problem. My contentPresenter tag look like this whose actually return the object of databinding.

<ContentPresenter
            ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
            Content="{TemplateBinding ContentControl.Content}"/>

Can anybody help me please? Im going crazy. Sorry for my english. Thanks.

A: 

"Class.Method.Property" is shown because WPF doesn't know how to do display your class. What you need is a DataTemplate for your class.

Unless you really need it, I wouldn't dig so deep in to the templates as you have in your examples.

If you have a databound ComboBox with objects and you just want to display a property of the bound object you can do:

 <ComboBox ItemsSource="{Binding PersonList}"
                  DisplayMemberPath="FullName" />

If you want a more advanced display, you can set the ItemTemplate.

<ComboBox ItemsSource="{Binding PersonList}">
            <ComboBox.ItemTemplate>
                <DataTemplate DataType="{x:Type local:Person}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding FullName}" />
                        <TextBlock Text="{Binding Age}" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
Tendlon
+1  A: 

I recently ran into the same issue and to fix it my content presenter now looks like this:

<ContentPresenter                            
    Content="{TemplateBinding ComboBox.SelectionBoxItem}"
    ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}"
    ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" /> 

My problem, much like yours, was the fact that I was missing the ContentTemplateSelector. Also if you are having issue with the drop down showing incorrectly as well, I found that in the ComboBoxItem style you can leave the content presenter blank (<ContentPresenter />) and it will display properly.

Scott Boettger
A: 

Nice!!!

The Scott Solution have solved my problem, it was crazying me. I tried, in my despair, to leave de comoboxItem Style without contentPresenter Tag, but i never thougth of it declare the contentPresenter tag without set up its propertys. I guess Wpf should set automatically.

Thank both.

Sidh
You should accept the answer. It gives you a better chance in the long run to get other questions answered.
Scott Boettger