Tutorial :WPF ComboBox displaying Object Type after styling



Question:

In my project I am using a ComboBox with my custom objects like Employees and using DisplayMemberPath="Description". This all works perfectly as expected.

Because this application is targeted to be used with a touchscreen, I had to style the scrollbar and make it wider which meant that I had to create a new template for the ScrolViewer. This also turned out quite nicely.

The problem comes in when I had to create a new ControlTemplate for the ComboBox which I need so that I can add the ScrollViewerControlTemplate to the ScrollViewer of the ComboBox. I used Blend to generate the ControlTemplate for me and then Bound the ScrollViewerControlTemplate to the ScrollViewer within the ComboBox ControlTemplate.

Excelent, only problem is that the ComboBox displays the correct text within the dropdown part of the control but the ToggleButton part of the control displays the Object Type which is [NameSpace].Employee.

I tried changing the ContentPresenter but without to much success.

<ContentPresenter                       Content="{TemplateBinding SelectionBoxItem}"                       ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"                       ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"                       Margin="{TemplateBinding Padding}"                       HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"                       VerticalAlignment="{TemplateBinding VerticalContentAlignment}"                       SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />  

Any help would be greatly appreciated.


Solution:1

In your ComboBox template, the content of that area should be bound to the SelectionBoxItem properties. So, if you are using a ContentPresenter to display the selection:

<ContentPresenter ContentSource="SelectionBoxItem" />  

If you are using a ToggleButton's content area, you would do something like so:

<ToggleButton Content="{TemplateBinding SelectionBoxItem}"                ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"                ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" />  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »