wpf ListBox 默认样式存档,方便手头没有 vs 时查阅.
1 <SolidColorBrush 2 x:Key="ListBox.Static.Background" 3 Color="#FFFFFFFF" /> 4 <SolidColorBrush 5 x:Key="ListBox.Static.Border" 6 Color="#FFABADB3" /> 7 <SolidColorBrush 8 x:Key="ListBox.Disabled.Background" 9 Color="#FFFFFFFF" /> 10 <SolidColorBrush 11 x:Key="ListBox.Disabled.Border" 12 Color="#FFD9D9D9" /> 13 14 <!-- ListBox 的样式 --> 15 <Style 16 x:Key="ListBoxStyle1" 17 TargetType="{x:Type ListBox}"> 18 <Setter Property="Background" Value="{StaticResource ListBox.Static.Background}" /> 19 <Setter Property="BorderBrush" Value="{StaticResource ListBox.Static.Border}" /> 20 <Setter Property="BorderThickness" Value="1" /> 21 <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" /> 22 <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" /> 23 <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" /> 24 <Setter Property="ScrollViewer.CanContentScroll" Value="true" /> 25 <Setter Property="ScrollViewer.PanningMode" Value="Both" /> 26 <Setter Property="Stylus.IsFlicksEnabled" Value="False" /> 27 <Setter Property="VerticalContentAlignment" Value="Center" /> 28 <Setter Property="Template"> 29 <Setter.Value> 30 <ControlTemplate TargetType="{x:Type ListBox}"> 31 <Border 32 x:Name="Bd" 33 Padding="1" 34 Background="{TemplateBinding Background}" 35 BorderBrush="{TemplateBinding BorderBrush}" 36 BorderThickness="{TemplateBinding BorderThickness}" 37 SnapsToDevicePixels="true"> 38 <ScrollViewer 39 Padding="{TemplateBinding Padding}" 40 Focusable="false"> 41 <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 42 </ScrollViewer> 43 </Border> 44 <ControlTemplate.Triggers> 45 <Trigger Property="IsEnabled" Value="false"> 46 <Setter TargetName="Bd" Property="Background" Value="{StaticResource ListBox.Disabled.Background}" /> 47 <Setter TargetName="Bd" Property="BorderBrush" Value="{StaticResource ListBox.Disabled.Border}" /> 48 </Trigger> 49 <MultiTrigger> 50 <MultiTrigger.Conditions> 51 <Condition Property="IsGrouping" Value="true" /> 52 <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false" /> 53 </MultiTrigger.Conditions> 54 <Setter Property="ScrollViewer.CanContentScroll" Value="false" /> 55 </MultiTrigger> 56 </ControlTemplate.Triggers> 57 </ControlTemplate> 58 </Setter.Value> 59 </Setter> 60 </Style> 61 62 <!-- ListBox 的布局面板 --> 63 <ItemsPanelTemplate x:Key="ListBoxItemsPanel1"> 64 <StackPanel /> 65 </ItemsPanelTemplate> 66 67 <Style x:Key="FocusVisual"> 68 <Setter Property="Control.Template"> 69 <Setter.Value> 70 <ControlTemplate> 71 <Rectangle 72 Margin="2" 73 SnapsToDevicePixels="true" 74 Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" 75 StrokeDashArray="1 2" 76 StrokeThickness="1" /> 77 </ControlTemplate> 78 </Setter.Value> 79 </Setter> 80 </Style> 81 <SolidColorBrush 82 x:Key="Item.MouseOver.Background" 83 Color="#1F26A0DA" /> 84 <SolidColorBrush 85 x:Key="Item.MouseOver.Border" 86 Color="#a826A0Da" /> 87 <SolidColorBrush 88 x:Key="Item.SelectedActive.Background" 89 Color="#3D26A0DA" /> 90 <SolidColorBrush 91 x:Key="Item.SelectedActive.Border" 92 Color="#FF26A0DA" /> 93 <SolidColorBrush 94 x:Key="Item.SelectedInactive.Background" 95 Color="#3DDADADA" /> 96 <SolidColorBrush 97 x:Key="Item.SelectedInactive.Border" 98 Color="#FFDADADA" /> 99 100 101 <!-- 每一项的容器样式 --> 102 <Style 103 x:Key="ListBoxItemContainerStyle1" 104 TargetType="{x:Type ListBoxItem}"> 105 <Setter Property="SnapsToDevicePixels" Value="True" /> 106 <Setter Property="Padding" Value="4,1" /> 107 <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" /> 108 <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" /> 109 <Setter Property="Background" Value="Transparent" /> 110 <Setter Property="BorderBrush" Value="Transparent" /> 111 <Setter Property="BorderThickness" Value="1" /> 112 <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}" /> 113 <Setter Property="Template"> 114 <Setter.Value> 115 <ControlTemplate TargetType="{x:Type ListBoxItem}"> 116 <Border 117 x:Name="Bd" 118 Padding="{TemplateBinding Padding}" 119 Background="{TemplateBinding Background}" 120 BorderBrush="{TemplateBinding BorderBrush}" 121 BorderThickness="{TemplateBinding BorderThickness}" 122 SnapsToDevicePixels="true"> 123 <ContentPresenter 124 HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 125 VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 126 SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 127 </Border> 128 <ControlTemplate.Triggers> 129 <MultiTrigger> 130 <MultiTrigger.Conditions> 131 <Condition Property="IsMouseOver" Value="True" /> 132 </MultiTrigger.Conditions> 133 <Setter TargetName="Bd" Property="Background" Value="{StaticResource Item.MouseOver.Background}" /> 134 <Setter TargetName="Bd" Property="BorderBrush" Value="{StaticResource Item.MouseOver.Border}" /> 135 </MultiTrigger> 136 <MultiTrigger> 137 <MultiTrigger.Conditions> 138 <Condition Property="Selector.IsSelectionActive" Value="False" /> 139 <Condition Property="IsSelected" Value="True" /> 140 </MultiTrigger.Conditions> 141 <Setter TargetName="Bd" Property="Background" Value="{StaticResource Item.SelectedInactive.Background}" /> 142 <Setter TargetName="Bd" Property="BorderBrush" Value="{StaticResource Item.SelectedInactive.Border}" /> 143 </MultiTrigger> 144 <MultiTrigger> 145 <MultiTrigger.Conditions> 146 <Condition Property="Selector.IsSelectionActive" Value="True" /> 147 <Condition Property="IsSelected" Value="True" /> 148 </MultiTrigger.Conditions> 149 <Setter TargetName="Bd" Property="Background" Value="{StaticResource Item.SelectedActive.Background}" /> 150 <Setter TargetName="Bd" Property="BorderBrush" Value="{StaticResource Item.SelectedActive.Border}" /> 151 </MultiTrigger> 152 <Trigger Property="IsEnabled" Value="False"> 153 <Setter TargetName="Bd" Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" /> 154 </Trigger> 155 </ControlTemplate.Triggers> 156 </ControlTemplate> 157 </Setter.Value> 158 </Setter> 159 </Style> 160 161 <!-- 每一项数据的模版 --> 162 <DataTemplate x:Key="ListBoxItemTemplate1"> 163 <Grid /> 164 </DataTemplate> 165 166 167 <!-- 使用方法 --> 168 <ListBox 169 ItemContainerStyle="{DynamicResource ListBoxItemContainerStyle1}" 170 ItemTemplate="{DynamicResource ListBoxItemTemplate1}" 171 ItemsPanel="{DynamicResource ListBoxItemsPanel1}" 172 Style="{DynamicResource ListBoxStyle1}" />