Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Making a simple RadioList with CheckBoxes

0.00/5 (No votes)
4 Apr 2012 1  
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

<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:

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

Add some content presenter nice props ..  

<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:

<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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here