Click here to Skip to main content
15,867,488 members
Articles / Desktop Programming / WPF
Tip/Trick

Making a simple RadioList with CheckBoxes

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
4 Apr 2012CPOL 6.3K   4  
How to make/use a simple RadioList displaying an "n" state value with checkboxes.

Introduction

Here is a nice Tip on how to make/use a simple RadioList displaying an "n" state value with checkboxes. 

Using the code 

Ok!-So because we need a radio list filled with checkboxes we need to define a custom RadiobuttonList Item. The targeted control for the Control Template should be set to ListBoxItem

XML
<Style x:Key="CustomRadioButtonListItem" TargetType="{x:Type ListBoxItem}" >
	<Setter Property="SnapsToDevicePixels" Value="true"/>
	<Setter Property="OverridesDefaultStyle" Value="true"/>
	<Setter Property="Template">
		<Setter.Value>
			<ControlTemplate TargetType="{x:Type ListBoxItem}"> 

Following a Grid content must be defined so that it contains the checkbox:

XML
<Grid Width="13" Height="13" >
<CheckBox IsChecked="{TemplateBinding IsSelected}" />
</Grid> 

Add some content presenter nice props ..  

XML
<ContentPresenter
	RecognizesAccessKey="True"
        Content             = "{TemplateBinding ContentControl.Content}"
        ContentTemplate     = "{TemplateBinding ContentControl.ContentTemplate}"
        ContentStringFormat = "{TemplateBinding ContentControl.ContentStringFormat}"
        HorizontalAlignment = "{TemplateBinding Control.HorizontalContentAlignment}"
        VerticalAlignment   = "{TemplateBinding Control.VerticalContentAlignment}"
        SnapsToDevicePixels = "{TemplateBinding UIElement.SnapsToDevicePixels}" />

Set the ItemContainerStyle prop of the ListBox to support the new RadiobuttonlistItem:

XML
<Style x:Key="CustomRadioButtonList" TargetType="ListBox">
	<Setter Property="ItemContainerStyle" Value="{StaticResource RadioButtonListItem}" />
</Style> 

.. and that's it.

Take care now only to override the style of your ListBox control when needed.

Points of Interest

So did I learn anything interesting/fun/annoying while writing the code? :)

Yep- pretty fun working with styles after I've got my way in the concept, but until then many "Why is that happening? It makes no sense!" thoughts crossed my mind. But it  pays off as styles are very useful when designing custom applications and as we already know could prove key when clients do want to buy. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Yonder
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --