Konrad Viltersten Konrad Viltersten - 1 year ago 115
C# Question

Custom Item Template for ComboBox Items

I had the following XAML looking just as I want it to look. It's basically a dropdown menu with checkboxes and some text to them.

<ComboBox x:Name="comboBox">

Then I learned that the options are possible to be dynamically adapted so I changed the markup and bound the combobox to an array. That works perfectly well functionality-wise but now I get only the names of the options, while the checkboxes are gone.

<ComboBox x:Name="comboBox"
ItemsSource="{x:Static local:MainWindow.AllKinds}">

So I'm looking for a way to create a custom item for populating the combobox but still so that the data is dynamically bound. I'm assuming WPF/XAML supports such functionality and I'm just too ignorant to know what to google for.

So my request is a rudimentary example or suggestion on what to google for. (Might be something seemingly obvious to everybody but me.)

Answer Source

So with most of the controls that offer up things from ItemSource there's a nifty built in feature that allows you to specify how each item should look/behave etc. So you can specify each Item in your ComboBox's by setting your own ItemTemplate.

Since a ComboBox just uses an ItemsControl to list its items, you can utilize this via ComboBox.ItemTemplate to make your items however you like, in this case probably something like (in pseudo).


Hope this helps, its some pretty handy info to know for when you want to start customizing Item generated lists between wpf, silverlight, wp, win-rt, etc. Cheers!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download