CheckBox and CheckedListBox controls in WPF
CheckBox:
Namespace : System.Windows.Controls.CheckBox
Designer Code:
<!--Window1.XAML-->
<CheckBox IsChecked="True" Checked="OnCheckedChanged">
Use this option</CheckBox>
Code Behind:
// Window1.xaml.cs
private void OnCheckedChanged(object sender, EventArgs e){
MessageBox.Show("CheckedChanged!");
}
Type : ContentControl
CheckBox From Codebehind
private void CheckBoxFromCodebehind()
{
CheckBox cbox1 = new CheckBox();
cbox1.Content = "Click me";
cbox1.IsChecked = true;
cbox1.IsChecked = true;
LayoutRoot.Children.Add(chb);
}
CheckedListBox:
Namespace : System.Windows.Controls.ListBox
Uses a DataTemplate to add a checkbox to the ListBoxItems.
Designer Code:
<UserControl x:Class="WPFTutorial.CheckedListBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<UserControl.Resources>
<DataTemplate x:Key="checkedListBox">
<CheckBox IsChecked="{Binding RelativeSource=/TemplatedParent
/TemplatedParent, Path=IsSelected, Mode=TwoWay}">
<ContentPresenter
Content="{TemplateBinding Property=Content}">
</ContentPresenter>
</CheckBox>
</DataTemplate>
</UserControl.Resources>
<UserControl.FixedTemplate>
<ListBox ItemTemplate="{StaticResource checkedListBox}"
ItemsSource="{Binding}"/>
</UserControl.FixedTemplate>
</UserControl>
Code Behind:
// Sample for populating the items via DataBinding:
public partial class CheckedListBox : UserControl {
public CheckedListBox() {
InitializeComponent();
this.DataContext = new string[]
{ "one", "two", "three", "four" };
}
}
Type : ItemsControl