65.9K
CodeProject is changing. Read more.
Home

How to Use Class System.Windows.Controls.ComboBoxItem to Implement a “Combobox with sub-list(s)”

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Jun 9, 2013

CPOL

1 min read

viewsIcon

10004

Using certain properties of System.Windows.Controls.ComboBoxItem, we can “split” Combobox list in several “sub-lists” separated by special ComboBoxItem instances that play role of “separators”

Introduction

In my tip, I described an approach I called a “Combobox with Most Recently Used sub-list”. It splits a Combobox content into 2 sub-lists - Most Recently Used Items and All Items.

Let’s see how this approach can be implemented in Silverlight.

Implementation

To split Combox content into sub-lists, we need to display the “separators” between them – in our case (http://www.codeproject.com/Tips/604305/A-Combobox-with-Most-Recently-Used-sub-list-Techni), these are the “Most Recently Used” and “All Countries” lines.

The class System.Windows.Controls.ComboBoxItem has the following properties that we can use to make a ComboBoxItem instance be such a “separator”:

  • IsEnabled: We set it to false and the item will become "disabled" – this is what we need for a “separator”.
  • HorizontalAlignment: We set it to HorizontalAlignment.Center and the item will become “aligned by center”, unlike the “regular” items aligned to the left.

So we insert two “special” items into the System.Windows.Controls.ComboBox.Items collection of a Combobox object.

One of them is a ComboBoxItem instance that has the following properties:

  • IsEnabled = false;
  • HorizontalAlignment = HorizontalAlignment.Center;
  • Content = "All Countries:"

And the second one has the following properties:

  • IsEnabled = false;
  • HorizontalAlignment = HorizontalAlignment.Center;
  • Content = "Most Recently Used:";

And here is the result – exactly what we need:

NOTE: Of course, this approach can be used not only to this specific task (a “Combobox with Most Recently Used and All Items sub-lists”); but to any task that requires to split a Combobox list to sub-list(s) by separating them with such “separators”.