Introduction
WPF doesn’t have a CheckedListBox control by default but with the WPF being so flexible it’s simple to make one just by changing the ItemsTemplate of a ListBox. This blog describes how to create CheckedListBox in WPF using XAML Code.
Getting Started
Windows Presentation Foundation provides various ways to customize existing controls. Here in this blog I have created a custom list box which contains Check Box as item. This Check Box item contains two types of image for checked and unchecked. Below in this.
When you select a Check Box, the image will appear which has a right symbol inside it for unchecked an image will appear without right symbol.
Now we will discuss, how I achieved the Check Box list box with image. For achieving above mentioned things, I took help of Item template, data template list box and Check Box style of WPF.
With help of Item template and data template of list box, I successfully placed Check Box inside list box item container.
The data template helped me to display bound value as text of Check Box in side item of list box item.
To display Check Box content (text and image), I customized the Check Box control template with help of Check Box style. In Check Box style I have customize the control template and placed two controls a text block for displaying text with Check Box and an image control to appear image in list box item.
I added one trigger for the property IsChecked, inside the trigger I customized control template again and placed a text block and image for displaying text and another image that indicates to selection of Check Box. Based on the result of IsChecked property, the trigger fire.
After applying above stuffs to list box and Check Box, I bound IsSelected property of list box item to IsChecked property of Check Box control to update its IsChecked property when list box item gets selected.
Go through the below code example for more details.
<ListBox Name="listBoxZone" >
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" x:Name="CheckBoxZone" IsChecked="{Binding Path=IsSelected, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="Width" Value="150"/>
<Setter Property="Height" Value="40"/>
<Setter Property="Margin" Value="4"/>
<Setter Property="Padding" Value="4,0,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Grid Background="Transparent" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<TextBlock Margin="5,10" Text="{Binding Path=Content,Mode=OneWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource TemplatedParent}}" />
<Image Grid.Column="1" Height="35" Width="35" Source="Images/download.png" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Grid Background="Transparent" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<TextBlock Margin="5,10" Text="{Binding Path=Content,Mode=OneWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource TemplatedParent}}" />
<Image Grid.Column="1" Height="35" Width="35" Source="Images/download1.png"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Thanks
Kailash Chandra Behera
Comments
Post a Comment