<ListBox>
<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="ListBoxItem.IsSelected" Value="True"/>
</Style>
</ListBox.Resources>
<ListBox>
未选择项目。 http://www.11011.net/archives/000692.html这个语句给出了一些提示,但ListBoxItem属于Control类的继承树。
我了解ItemContainerStyle属性。我只是好奇为什么没有应用风格。
UPDATE1:
它确实适用于ListBox,但不适用于具有默认样式的派生类:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wcl="clr-namespace:SecurityFramework.UI.WpfControlsLibrary">
<Style TargetType="{x:Type wcl:ListBoxEx}">
<Setter Property="SelectAllToggleStyle">
<Setter.Value>
<Style TargetType="ToggleButton">
<Setter Property="Content" Value="Select All"/>
<Setter Property="Focusable" Value="True"/>
<Setter Property="Margin" Value="2,0,0,0"/>
<Setter Property="IsThreeState" Value="False"/>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type wcl:ListBoxEx}">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="true"
Padding="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
Height="{TemplateBinding Height}"
Width="{TemplateBinding Width}">
<DockPanel VerticalAlignment="Top" HorizontalAlignment="Stretch" LastChildFill="True">
<CheckBox Name="PART_SelectAllToggle" DockPanel.Dock="Top"/>
<Separator Margin="4" Visibility="{Binding Path=Visibility, ElementName=PART_SelectAllToggle}" DockPanel.Dock="Top"/>
<ScrollViewer>
<ItemsPresenter DockPanel.Dock="Top"/>
</ScrollViewer>
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style>
<Setter Property="ListBoxItem.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border
SnapsToDevicePixels="true"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="0"
VerticalAlignment="{TemplateBinding VerticalAlignment}"
HorizontalAlignment="Stretch"
Height="{TemplateBinding Height}"
Width="{TemplateBinding Width}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource AncestorType=wcl:ListBoxEx, Mode=FindAncestor}, Path=ItemBackground}"/>
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType=wcl:ListBoxEx, Mode=FindAncestor}, Path=ItemForeground}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{x:Static SystemColors.HighlightBrush}"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource AncestorType=wcl:ListBoxEx, Mode=FindAncestor}, Path=SelectedItemBackground}"/>
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType=wcl:ListBoxEx, Mode=FindAncestor}, Path=SelectedItemForeground}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Control.VerticalAlignment" Value="Center"/>
<Setter Property="Control.FocusVisualStyle">
<Setter.Value>
<Style TargetType="Control">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="0" StrokeThickness="2" Stroke="Black" SnapsToDevicePixels="true"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Padding" Value="4"/>
<Setter Property="SelectedItemBackground" Value="{x:Static SystemColors.HighlightBrush}"/>
<Setter Property="SelectedItemForeground" Value="White"/>
<Setter Property="ItemBackground" Value="Transparent"/>
<Setter Property="ItemForeground" Value="Black"/>
<Setter Property="MouseOverItemBackground" Value="LightGray"/>
<Setter Property="MouseOverItemForeground" Value="Black"/>
</Style>
</ResourceDictionary>
UPDATE2:
并行线程:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f7f0558b-c041-492e-bd96-896f97a7b07e/