PropertyGrid: CheckBoxList Property

This entry is part 5 of 5 in the series C#: Property Grid

Build a CheckBoxList Property Type

 

 

public class CheckBoxList : INotifyPropertyChanged
{
	private dynamic _values;
	private dynamic selectedItem;
	public event PropertyChangedEventHandler PropertyChanged;
	public event PropertyValueChangedEventHandler PropertyValueChanged;

	public CheckBoxList(String name, PropertyGrid pg)
	{
		PropertyName = name;
		PG = pg;
	}

	private String PropertyName { get; set; }
	private PropertyGrid PG { get; set; }

	public dynamic Values
	{
		get
		{
			return _values;
		}
		set
		{
			if (value != null)
			_values = value;
		}
	}

	public string ValueMember { get; set; }
	public string DisplayMember { get; set; }

	[Browsable(false)]
	public dynamic SelectedItem
	{
		get
		{
			return selectedItem;
		}
		set
		{
			String oldValue = selectedItem;
			selectedItem = value;
	
			if (PropertyChanged != null)
				PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));

			if (PG != null)
			{
				if (PropertyValueChanged != null)
					PropertyValueChanged(this, new PropertyValueChangedEventArgs(PG.SelectedGridItem, oldValue));
			}
		}
	}

	public override string ToString()
	{
		return SelectedItem;
	}
}

PropertyGrid: CheckBoxList Editor

This entry is part 4 of 5 in the series C#: Property Grid

You may want to add a checkboxlist to your property grid. If you do then the below code is what you will need to create the editor.

Build the CheckBox List Editor

 public class CheckBoxListEditor : System.Drawing.Design.UITypeEditor
{
      private IWindowsFormsEditorService es = null;
      
      public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
      {
            return UITypeEditorEditStyle.DropDown;
      }

      public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
      {
            if (provider != null)
            {
                  // This service is in charge of popping our ListBox.
                  es = ((IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)));

                  if (es != null && value is CheckBoxList)
                  {
                        var property = (CheckBoxList)value;
                        var cl = new CheckedListBox();
                        cl.CheckOnClick = true;

                        Dictionary<String, Boolean> vals = new Dictionary<string, bool>();

                        foreach (KeyValuePair<String, Boolean> kvp in property.Values)
                        {
                              vals[kvp.Key] = kvp.Value;
                              cl.Items.Add(kvp.Key, kvp.Value);
                        }

                        // Drop the list control.
                        es.DropDownControl(cl);

                        if (cl.SelectedItem != null )
                        {
                              vals[cl.SelectedItem.ToString()] = !vals[cl.SelectedItem.ToString()];
                              property.Values = vals;
                              property.SelectedItem = String.Join(", ", (from v in vals where v.Value == true select v.Key).ToList());
                              value = property;
                        }
                  }
            }

            return value;
      }
}