Many developers are frustrated with lack of basic WPF controls. The WPF and WPF toolkit have some important and cool controls. However, some basic and necessary controls are still missing. But it is easy to create the basic controls in WPF. The article will show you how to create a checklistbox in WPF.
Here, I will create a custom control which will inherit from Listbox. The following code snippet shows the entire source code for the CheckListBox.
namespace MyControlLibrary
{
public class CheckListBox:ListBox
{
public CheckListBox()
{
}
/// /// Item Source of the check list box
/// public new IEnumerable ItemsSource
{
get
{
return this.ItemsSource;
}
set
{
//set the template
this.ItemTemplate = GetTemplate();
base.ItemsSource = value;
}
}
/// /// Gets or sets the property of source object. The property value will be displayed as a content of the checkbox.
/// public string DisplayMember { get; set; }
/// ///
/// public string ValueMember { get; set; }
/// /// returns the template for the checkbox.
/// /// private DataTemplate GetTemplate()
{
DataTemplate template = new DataTemplate();
Binding binding = new Binding(this.DisplayMember);
FrameworkElementFactory fwfactory = new FrameworkElementFactory(typeof(CheckBox));
fwfactory.SetValue(CheckBox.NameProperty, "chk");
fwfactory.SetValue(CheckBox.ContentProperty, binding);
fwfactory.SetValue(CheckBox.TagProperty, new Binding(this.ValueMember));
template.VisualTree = fwfactory;
return template;
}
/// /// returns the checked list items.
/// public IEnumerable GetCheckListItems
{
get
{
List