Combobox binding in WPF

I am new to wpf. Actually, I am a style ComboBoxthat gets data from a database. ComboBoxworks great at this point without style.

I edited the ControlTemplatecontrol ComboboxItemin the stylesheet, for example, in the resource dictionary.

If Styleapplied to ComboBoxwith static data, the style works fine. But if the style is applied to ComboBoxwith dynamic data (database binding in this case), the list of elements returns an object only (the elements shown are similar to "Class.Method.Property"), but not the content property that I need to show.

I tried everything and I read everything on the web about xaml styles and templates ComboBox, but I could not solve the problem.

My ContentPresenterlooks like it returns a data binding object:

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

Can anybody help me?

+3
source share
2 answers

"Class.Method.Property" is shown because WPF does not know how to render your class. You need a DataTemplate for your class.

If you really do not need this, I would not go so deep into the templates as in your examples.

If you have a ComboBox data package with objects, and you just want to display a property of the associated object that you can do:

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

If you want a more advanced display, you can set 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>
+4
source

, - :

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

, , , ContentTemplateSelector. , , , ComboBoxItem - (<ContentPresenter />), .

+4

Source: https://habr.com/ru/post/1765278/


All Articles