WPF版チェックボックス(System.Windows.Controls.CheckBoxのほう)のボックス部分を任意のイメージにする方法を調べてみた。
参考にした情報。
- Silverlight/WPFで使える逆引きTips集 ――ラジオボタンとチェックボックス機能 (4/7):CodeZine(コードジン)
- Is it possible to style a BulletDecorator in WPF? - Stack Overflow
Microsoft公式の情報を使っていないというね…。
ソースコード (XAML)
<Window x:Class="CustomCheckBox2013_06_06.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="200" Width="180"> <Window.Resources> <!--おまけ--> <Style x:Key="customcheckboxstyle1" TargetType="{x:Type CheckBox}"> <Setter Property="Margin" Value="10, 1, 10, 1" /> </Style> <Style x:Key="customcontentpresenterstyle1" TargetType="{x:Type ContentPresenter}"> <Setter Property="Margin" Value="5, 0, 0, 0" /> </Style> <!--ここまでおまけ--> <ControlTemplate x:Key="customcheckbox1" TargetType="{x:Type CheckBox}"> <BulletDecorator> <BulletDecorator.Bullet> <Image Name="TickImage" Source="/CustomCheckBox2013-06-06;component/Resources/Image0.png" Width="23" Height="23" /> </BulletDecorator.Bullet> <ContentPresenter VerticalAlignment="Center" Style="{StaticResource customcontentpresenterstyle1}" /> </BulletDecorator> <ControlTemplate.Triggers> <!--オン状態のイメージ--> <Trigger Property="IsChecked" Value="True"> <Setter TargetName="TickImage" Property="Source" Value="/CustomCheckBox2013-06-06;component/Resources/Image1.png" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Window.Resources> <StackPanel> <CheckBox>Default Style</CheckBox> <CheckBox Template="{StaticResource customcheckbox1}" IsChecked="True">Custom CheckBox 1</CheckBox> <CheckBox Template="{StaticResource customcheckbox1}">Custom CheckBox 2</CheckBox> <CheckBox Template="{StaticResource customcheckbox1}" Style="{StaticResource customcheckboxstyle1}" IsChecked="True">Custom CheckBox 3</CheckBox> <CheckBox Template="{StaticResource customcheckbox1}" Style="{StaticResource customcheckboxstyle1}" IsChecked="False">Custom CheckBox 4</CheckBox> </StackPanel> </Window>
プロジェクトファイル (Visual Studio 2010)
CustomCheckBox2013-06-06.zip
レンダリング結果
備考
この例で使っているのは ".NET Framework 4 Client Profile".