Click here to Skip to main content
Click here to Skip to main content

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

, 9 Jun 2013
Rate this:
Please Sign up or sign in to vote.
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”.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

VictorSotnikov
Software Developer
Belarus Belarus
I am a .NET and MS SQL developer. See my personal website https://sites.google.com/site/vicsotnikov

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 9 Jun 2013
Article Copyright 2013 by VictorSotnikov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid