RadioButton を ListBox で実装する、RadioButton を ListBox で実装する 2の続き。
カスタムコントロールにしてみた。
RadioButtonsControl.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfSample
{
/// <summary>
/// 選択可能な項目をラジオボタンで選択するコントロール
/// </summary>
public class RadioButtonsControl : ListBox
{
/// <summary>
/// クラスの初期化
/// </summary>
static RadioButtonsControl ()
{
// プロパティのデフォルト値を上書きする
DefaultStyleKeyProperty.OverrideMetadata (typeof (RadioButtonsControl), new FrameworkPropertyMetadata (typeof (RadioButtonsControl)));
BackgroundProperty.OverrideMetadata (typeof (RadioButtonsControl), new FrameworkPropertyMetadata (Brushes.Transparent));
BorderBrushProperty.OverrideMetadata (typeof (RadioButtonsControl), new FrameworkPropertyMetadata (Brushes.Transparent));
}
/// <summary>
/// コントロール内にラジオボタンを並べる列数を取得または設定します。これは、依存関係プロパティです。
/// </summary>
/// <value>コントロール内の列の数。既定値は 0 です。</value>
public int Columns
{
get { return (int) GetValue (ColumnsProperty); }
set { SetValue (ColumnsProperty, value); }
}
/// <summary>
/// RadioButtonControl.Columns 依存関係プロパティを識別します。
/// </summary>
public static readonly DependencyProperty ColumnsProperty =
DependencyProperty.Register ("Columns", typeof (int), typeof (RadioButtonsControl), new FrameworkPropertyMetadata (0));
/// <summary>
/// コントロール内にラジオボタンを並べる行数を取得または設定します。これは、依存関係プロパティです。
/// </summary>
/// <value>コントロール内の行の数。既定値は 0 です。</value>
public int Rows
{
get { return (int) GetValue (RowsProperty); }
set { SetValue (RowsProperty, value); }
}
/// <summary>
/// RadioButtonControl.Rows 依存関係プロパティを識別します。
/// </summary>
public static readonly DependencyProperty RowsProperty =
DependencyProperty.Register ("Rows", typeof (int), typeof (RadioButtonsControl), new FrameworkPropertyMetadata (0));
}
}
Generic.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfSample">
<Style TargetType="{x:Type local:RadioButtonsControl}">
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.CanContentScroll" Value="true" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="SelectionMode" Value="Single" />
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<UniformGrid
Columns="{Binding Path=Columns, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:RadioButtonsControl}}}"
Rows="{Binding Path=Rows, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:RadioButtonsControl}}}" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Margin" Value="5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<RadioButton
IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}}">
<ContentPresenter />
</RadioButton>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:RadioButtonsControl}">
<Border SnapsToDevicePixels="True" x:Name="Bd" Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" Padding="1">
<ScrollViewer Focusable="False" Padding="{TemplateBinding Padding}">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>