Declare the object of
ItemPanelTemplate class and set the VisualTree property to
FrameworkElementFactory, again set it to the ItemsPanelTemplate
of list box. See the bellow code.
protected
override void
OnInitialized(EventArgs e)
{
base.OnInitialized(e);
FrameworkElementFactory
Factory = new FrameworkElementFactory(typeof(WrapPanel));
Factory.SetValue(WrapPanel.OrientationProperty,
Orientation.Horizontal);
Factory.SetValue(CheckBox.MaxWidthProperty,
this.Width);
Factory.SetValue(CheckBox.MinHeightProperty,
this.Height);
Factory.SetValue(WrapPanel.IsItemsHostProperty,
true);
ItemsPanelTemplate
IPT = new ItemsPanelTemplate();
IPT.VisualTree =
Factory;
this.ItemsPanel
= IPT;
}
Again override the OnItemSourceChenged
,do the same step like 6, 7 and 8. Create the object of
FrameworkElementFactory class and pass the CheckBox class as type
to the parameter of FrameworkElementFactory's Contracture. Set the CheckBox
properties like name, width and add Click event. See the bellow code:
FrameworkElementFactory
FEF = new FrameworkElementFactory(typeof(CheckBox));
FEF.SetValue(CheckBox.NameProperty, "txtChecked");
FEF.SetValue(CheckBox.WidthProperty, 50.00);
FEF.AddHandler(CheckBox.ClickEvent, new
RoutedEventHandler(CheckBox_CheckChanged), true);
if (!string.IsNullOrEmpty(this.ContentPath))
{
Binding CBG =
new Binding(this.ContentPath);
FEF.SetBinding(CheckBox.ContentProperty,
CBG);
}
if (!string.IsNullOrEmpty(this.IsSelectPath))
{
Binding SBG =
new Binding();
SBG.Path =
new PropertyPath(this.IsSelectPath);
SBG.Mode =
BindingMode.TwoWay;
SBG.UpdateSourceTrigger
= UpdateSourceTrigger.PropertyChanged;
FEF.SetBinding(CheckBox.IsCheckedProperty,
SBG);
}
DataTemplate IT =
new DataTemplate();
IT.VisualTree = FEF;
this.ItemTemplate
= IT;
If you covered all the 10 steps your
CheckBoxList is done,.
Code of the Class
public
class
CheckListBox : ListBox
{
//Properties of Parent
new private
string DisplayMemberPath {
get; set; }
new private
string SelectedValuePath {
get; set; }
public string
ContentPath { get;
set; }
public string
IsSelectPath { get;
set; }
protected override
void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
FrameworkElementFactory Factory = new
FrameworkElementFactory(typeof(WrapPanel));
Factory.SetValue(WrapPanel.OrientationProperty, Orientation.Horizontal);
Factory.SetValue(CheckBox.MaxWidthProperty, this.Width);
Factory.SetValue(CheckBox.MinHeightProperty, this.Height);
Factory.SetValue(WrapPanel.IsItemsHostProperty,
true);
ItemsPanelTemplate IPT = new
ItemsPanelTemplate();
IPT.VisualTree =
Factory;
this.ItemsPanel = IPT;
}
protected override
void
OnItemsSourceChanged(System.Collections.IEnumerable
oldValue, System.Collections.IEnumerable
newValue)
{
base.OnItemsSourceChanged(oldValue, newValue);
FrameworkElementFactory FEF = new
FrameworkElementFactory(typeof(CheckBox));
FEF.SetValue(CheckBox.NameProperty,
"txtChecked");
FEF.SetValue(CheckBox.WidthProperty, 50.00);
FEF.AddHandler(CheckBox.ClickEvent, new
RoutedEventHandler(CheckBox_CheckChanged), true);
if (!string.IsNullOrEmpty(this.ContentPath))
{
Binding CBG =
new Binding(this.ContentPath);
FEF.SetBinding(CheckBox.ContentProperty, CBG);
}
if (!string.IsNullOrEmpty(this.IsSelectPath))
{
Binding SBG =
new Binding();
SBG.Path =
new PropertyPath(this.IsSelectPath);
SBG.Mode =
BindingMode.TwoWay;
SBG.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
FEF.SetBinding(CheckBox.IsCheckedProperty, SBG);
}
DataTemplate IT =
new DataTemplate();
IT.VisualTree =
FEF;
this.ItemTemplate = IT;
}
private void
CheckBox_CheckChanged(object sender,
RoutedEventArgs e)
{
CheckBox CB =
(CheckBox)sender;
ContentPresenter
CP = (ContentPresenter)CB.TemplatedParent;
ListBoxItem LBI =
(ListBoxItem)CP.TemplatedParent;
LBI.IsSelected =
(bool)CB.IsChecked;
e.Handled =
true;
}
}