Source and Project
以前、作成したCheckedListBoxに思わぬ挙動をすることがわかったので修正版を作成してみた。
【WPF】CheckedListBoxをカスタムコントロールで作成する。
http://pro.art55.jp/?eid=1069565
上記のURLでは下記のように書いていました。
<Style TargetType="{x:Type local:MissCheckedListBox}"
BasedOn="{StaticResource {x:Type ListBox}}">
<Setter Property="SelectionMode" Value="Multiple" />
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<CheckBox Focusable="False"
SnapsToDevicePixels="True"
Foreground="{TemplateBinding Foreground}"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"
FontStyle="{TemplateBinding FontStyle}"
FontWeight="{TemplateBinding FontWeight}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
<ContentPresenter/>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
注目するところはCheckBoxにフォーカスが移動しないようにしていました。
これを下記にようにかきかえました。
<Style TargetType="{x:Type local:CheckedListBox}"
BasedOn="{StaticResource {x:Type ListBox}}">
<Setter Property="SelectionMode" Value="Multiple" />
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Focusable" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<CheckBox Focusable="True"
SnapsToDevicePixels="True"
Foreground="{TemplateBinding Foreground}"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"
FontStyle="{TemplateBinding FontStyle}"
FontWeight="{TemplateBinding FontWeight}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
<ContentPresenter/>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
こっちはListBoxItemにフォーカス移動がないようにして、CheckBoxにフォーカス移動可能にしています。この微妙な違いは、フォーカス移動に微妙な違いをもたらします。
どう違うかは実際に実行してみて試してみてください。上はクリックするとフォーカスが移動しますが、下は移動しません。
Source and Project
- 2010.05.25 Tuesday
- WPF
- 22:13
- comments(1)
- trackbacks(0)


- by art55


















