WPF ListView 样式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <Style x:Key="{x:Static GridView.GridViewScrollViewerStyleKey}" TargetType="{x:Type ScrollViewer}"> 2 <Setter Property="Focusable" Value="false"/> 3 <Setter Property="Template"> 4 <Setter.Value> 5 <ControlTemplate TargetType="{x:Type ScrollViewer}"> 6 <Grid Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> 7 <Grid.ColumnDefinitions> 8 <ColumnDefinition Width="*"/> 9 <ColumnDefinition Width="Auto"/> 10 </Grid.ColumnDefinitions> 11 <Grid.RowDefinitions> 12 <RowDefinition Height="*"/> 13 <RowDefinition Height="Auto"/> 14 </Grid.RowDefinitions> 15 <DockPanel Margin="{TemplateBinding Padding}"> 16 <ScrollViewer DockPanel.Dock="Top" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"> 17 <GridViewHeaderRowPresenter AllowsColumnReorder="{Binding TemplatedParent.View.AllowsColumnReorder, RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderContainerStyle="{Binding TemplatedParent.View.ColumnHeaderContainerStyle, RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderToolTip="{Binding TemplatedParent.View.ColumnHeaderToolTip, RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderStringFormat="{Binding TemplatedParent.View.ColumnHeaderStringFormat, RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderContextMenu="{Binding TemplatedParent.View.ColumnHeaderContextMenu, RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderTemplate="{Binding TemplatedParent.View.ColumnHeaderTemplate, RelativeSource={RelativeSource TemplatedParent}}" Columns="{Binding TemplatedParent.View.Columns, RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderTemplateSelector="{Binding TemplatedParent.View.ColumnHeaderTemplateSelector, RelativeSource={RelativeSource TemplatedParent}}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 18 </ScrollViewer> 19 <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" KeyboardNavigation.DirectionalNavigation="Local" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 20 </DockPanel> 21 <ScrollBar x:Name="PART_HorizontalScrollBar" Cursor="Arrow" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0.0" Orientation="Horizontal" Grid.Row="1" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}" Style="{DynamicResource ScrollBarStyle}"/> 22 <ScrollBar x:Name="PART_VerticalScrollBar" Cursor="Arrow" Grid.Column="1" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0.0" Orientation="Vertical" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}" Style="{DynamicResource ScrollBarStyle}"/> 23 </Grid> 24 </ControlTemplate> 25 </Setter.Value> 26 </Setter> 27 </Style> 28 <Style x:Key="ListViewStyle1" TargetType="{x:Type ListView}"> 29 <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/> 30 <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 31 <Setter Property="ScrollViewer.CanContentScroll" Value="true"/> 32 <Setter Property="ScrollViewer.PanningMode" Value="Both"/> 33 <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> 34 <Setter Property="Template"> 35 <Setter.Value> 36 <ControlTemplate TargetType="{x:Type ListView}"> 37 <ScrollViewer Padding="{TemplateBinding Padding}" Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}"> 38 <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 39 </ScrollViewer> 40 <ControlTemplate.Triggers> 41 <MultiTrigger> 42 <MultiTrigger.Conditions> 43 <Condition Property="IsGrouping" Value="true"/> 44 <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/> 45 </MultiTrigger.Conditions> 46 <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 47 </MultiTrigger> 48 </ControlTemplate.Triggers> 49 </ControlTemplate> 50 </Setter.Value> 51 </Setter> 52 </Style> 53 <Style TargetType="{x:Type ListViewItem}"> 54 <Setter Property="Template" > 55 <Setter.Value> 56 <ControlTemplate TargetType="{x:Type ListBoxItem}"> 57 <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True"> 58 <GridViewRowPresenter VerticalAlignment="Center" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 59 </Border> 60 <ControlTemplate.Triggers> 61 <Trigger Property="IsSelected" Value="True"> 62 <Setter Property="BorderThickness" TargetName="Bd" Value="2"></Setter> 63 <Setter Property="BorderBrush" TargetName="Bd" Value="#FF866C"></Setter> 64 </Trigger> 65 <Trigger Property="IsMouseOver" Value="True"> 66 <Setter Property="BorderThickness" TargetName="Bd" Value="2"></Setter> 67 <Setter Property="BorderBrush" TargetName="Bd" Value="#FFEAE4"></Setter> 68 </Trigger> 69 <Trigger Property="Height" Value="Auto"> 70 <Setter Property="MinHeight" Value="35" /> 71 </Trigger> 72 <Trigger Property="IsEnabled" Value="False"> 73 <Setter Property="Opacity" Value="0.5"/> 74 </Trigger> 75 </ControlTemplate.Triggers> 76 </ControlTemplate> 77 </Setter.Value> 78 </Setter> 79 </Style> 80 <Style TargetType="{x:Type GridViewColumnHeader}"> 81 <Setter Property="Background" Value="#FFEAE4" /> 82 <Setter Property="Template" > 83 <Setter.Value> 84 <ControlTemplate TargetType="{x:Type GridViewColumnHeader}"> 85 <Grid SnapsToDevicePixels="True" > 86 <Border x:Name="HeaderBorder" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"> 87 <Grid> 88 <Grid.RowDefinitions> 89 <RowDefinition MaxHeight="7"/> 90 <RowDefinition/> 91 </Grid.RowDefinitions> 92 <Border Padding="{TemplateBinding Padding}" Grid.RowSpan="2"> 93 <ContentPresenter x:Name="HeaderContent" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 94 </Border> 95 </Grid> 96 </Border> 97 <Canvas> 98 <Thumb x:Name="PART_HeaderGripper"> 99 <Thumb.Style> 100 <Style TargetType="{x:Type Thumb}"> 101 <Setter Property="Canvas.Right" Value="-9"/> 102 <Setter Property="Width" Value="18"/> 103 <Setter Property="Height" Value="{Binding ActualHeight, RelativeSource={RelativeSource TemplatedParent}}"/> 104 <Setter Property="Padding" Value="0"/> 105 <Setter Property="Background" Value="White"/> 106 <Setter Property="Template"> 107 <Setter.Value> 108 <ControlTemplate TargetType="{x:Type Thumb}"> 109 <Border Background="Transparent" Padding="{TemplateBinding Padding}"> 110 <Rectangle Fill="{TemplateBinding Background}" HorizontalAlignment="Center" Width="1"/> 111 </Border> 112 </ControlTemplate> 113 </Setter.Value> 114 </Setter> 115 </Style> 116 </Thumb.Style> 117 </Thumb> 118 </Canvas> 119 </Grid> 120 <ControlTemplate.Triggers> 121 <Trigger Property="IsMouseOver" Value="True"> 122 <Setter Property="Background" TargetName="HeaderBorder" Value="#FF866B"/> 123 <Setter Property="Background" TargetName="PART_HeaderGripper" Value="Transparent"/> 124 </Trigger> 125 <Trigger Property="IsPressed" Value="True"> 126 <Setter Property="Background" TargetName="HeaderBorder" Value="#FF866B"/> 127 <Setter Property="Visibility" TargetName="PART_HeaderGripper" Value="Hidden"/> 128 </Trigger> 129 <Trigger Property="Height" Value="Auto"> 130 <Setter Property="MinHeight" Value="35"/> 131 </Trigger> 132 <Trigger Property="IsEnabled" Value="False"> 133 <Setter Property="Opacity" Value="0.5"/> 134 </Trigger> 135 </ControlTemplate.Triggers> 136 </ControlTemplate> 137 </Setter.Value> 138 </Setter> 139 </Style> 140 <Style x:Key="VerticalScrollBarPageButton" TargetType="{x:Type RepeatButton}"> 141 <Setter Property="OverridesDefaultStyle" Value="true"/> 142 <Setter Property="Background" Value="Transparent"/> 143 <Setter Property="Focusable" Value="false"/> 144 <Setter Property="IsTabStop" Value="false"/> 145 <Setter Property="Template"> 146 <Setter.Value> 147 <ControlTemplate TargetType="{x:Type RepeatButton}"> 148 <Rectangle Fill="{TemplateBinding Background}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"/> 149 </ControlTemplate> 150 </Setter.Value> 151 </Setter> 152 </Style> 153 <Style x:Key="VerticalScrollBarThumb" TargetType="{x:Type Thumb}"> 154 <Setter Property="OverridesDefaultStyle" Value="true"/> 155 <Setter Property="IsTabStop" Value="false"/> 156 <Setter Property="Template"> 157 <Setter.Value> 158 <ControlTemplate TargetType="{x:Type Thumb}"> 159 <Border x:Name="bd" Width="9" CornerRadius="5" Background="#D3D3D3"/> 160 <ControlTemplate.Triggers> 161 <Trigger Property="IsMouseOver" Value="True"> 162 <Setter Property="Background" TargetName="bd" Value="DarkGray" /> 163 </Trigger> 164 </ControlTemplate.Triggers> 165 </ControlTemplate> 166 </Setter.Value> 167 </Setter> 168 </Style> 169 <Style x:Key="HorizontalScrollBarThumb" TargetType="{x:Type Thumb}"> 170 <Setter Property="OverridesDefaultStyle" Value="true"/> 171 <Setter Property="IsTabStop" Value="false"/> 172 <Setter Property="Template"> 173 <Setter.Value> 174 <ControlTemplate TargetType="{x:Type Thumb}"> 175 <Border x:Name="bd" Height="9" CornerRadius="5" Background="#D3D3D3"/> 176 <ControlTemplate.Triggers> 177 <Trigger Property="IsMouseOver" Value="True"> 178 <Setter Property="Background" TargetName="bd" Value="DarkGray" /> 179 </Trigger> 180 </ControlTemplate.Triggers> 181 </ControlTemplate> 182 </Setter.Value> 183 </Setter> 184 </Style> 185 <Style x:Key="HorizontalScrollBarPageButton" TargetType="{x:Type RepeatButton}"> 186 <Setter Property="OverridesDefaultStyle" Value="true"/> 187 <Setter Property="Background" Value="Transparent"/> 188 <Setter Property="Focusable" Value="false"/> 189 <Setter Property="IsTabStop" Value="false"/> 190 <Setter Property="Template"> 191 <Setter.Value> 192 <ControlTemplate TargetType="{x:Type RepeatButton}"> 193 <Rectangle Fill="{TemplateBinding Background}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"/> 194 </ControlTemplate> 195 </Setter.Value> 196 </Setter> 197 </Style> 198 199 <Style x:Key="VerticalScrollBarButtomButton" TargetType="{x:Type RepeatButton}"> 200 <Setter Property="OverridesDefaultStyle" Value="true"/> 201 <Setter Property="Focusable" Value="false"/> 202 <Setter Property="IsTabStop" Value="false"/> 203 <Setter Property="Template"> 204 <Setter.Value> 205 <ControlTemplate TargetType="{x:Type RepeatButton}"> 206 <Path x:Name="path" Data="M 4,6 L 13,6 L 8.5,13 Z" Fill="#D3D3D3" Stroke="#D3D3D3" /> 207 <ControlTemplate.Triggers> 208 <Trigger Property="IsMouseOver" Value="True"> 209 <Setter Property="Fill" TargetName="path" Value="DarkGray" /> 210 <Setter Property="Stroke" TargetName="path" Value="DarkGray" /> 211 </Trigger> 212 <Trigger Property="IsPressed" Value="True"> 213 <Setter Property="Fill" TargetName="path" Value="DarkGray" /> 214 <Setter Property="Stroke" TargetName="path" Value="DarkGray" /> 215 </Trigger> 216 </ControlTemplate.Triggers> 217 </ControlTemplate> 218 </Setter.Value> 219 </Setter> 220 </Style> 221 <Style x:Key="VerticalScrollBarTopButton" TargetType="{x:Type RepeatButton}"> 222 <Setter Property="OverridesDefaultStyle" Value="true"/> 223 <Setter Property="Focusable" Value="false"/> 224 <Setter Property="IsTabStop" Value="false"/> 225 <Setter Property="Template"> 226 <Setter.Value> 227 <ControlTemplate TargetType="{x:Type RepeatButton}"> 228 <Path x:Name="path" Data="M 4,10 L 13,10 L 8.5,3 Z" Fill="#D3D3D3" Stroke="#D3D3D3" /> 229 <ControlTemplate.Triggers> 230 <Trigger Property="IsMouseOver" Value="True"> 231 <Setter Property="Fill" TargetName="path" Value="DarkGray" /> 232 <Setter Property="Stroke" TargetName="path" Value="DarkGray" /> 233 </Trigger> 234 <Trigger Property="IsPressed" Value="True"> 235 <Setter Property="Fill" TargetName="path" Value="DarkGray" /> 236 <Setter Property="Stroke" TargetName="path" Value="DarkGray" /> 237 </Trigger> 238 </ControlTemplate.Triggers> 239 </ControlTemplate> 240 </Setter.Value> 241 </Setter> 242 </Style> 243 <Style x:Key="HorizontalScrollBarLeftButton" TargetType="{x:Type RepeatButton}"> 244 <Setter Property="OverridesDefaultStyle" Value="true"/> 245 <Setter Property="Focusable" Value="false"/> 246 <Setter Property="IsTabStop" Value="false"/> 247 <Setter Property="Template"> 248 <Setter.Value> 249 <ControlTemplate TargetType="{x:Type RepeatButton}"> 250 <Path x:Name="path" Data="M 10,4 L 10,13 L 3, 8.5 Z" Fill="#D3D3D3" Stroke="#D3D3D3" /> 251 <ControlTemplate.Triggers> 252 <Trigger Property="IsMouseOver" Value="True"> 253 <Setter Property="Fill" TargetName="path" Value="DarkGray" /> 254 <Setter Property="Stroke" TargetName="path" Value="DarkGray" /> 255 </Trigger> 256 <Trigger Property="IsPressed" Value="True"> 257 <Setter Property="Fill" TargetName="path" Value="DarkGray" /> 258 <Setter Property="Stroke" TargetName="path" Value="DarkGray" /> 259 </Trigger> 260 </ControlTemplate.Triggers> 261 </ControlTemplate> 262 </Setter.Value> 263 </Setter> 264 </Style> 265 <Style x:Key="HorizontalScrollBarRightButton" TargetType="{x:Type RepeatButton}"> 266 <Setter Property="OverridesDefaultStyle" Value="true"/> 267 <Setter Property="Focusable" Value="false"/> 268 <Setter Property="IsTabStop" Value="false"/> 269 <Setter Property="Template"> 270 <Setter.Value> 271 <ControlTemplate TargetType="{x:Type RepeatButton}"> 272 <Path x:Name="path" Data="M 6,4 L 6,13 L 13, 8.5 Z" Fill="#D3D3D3" Stroke="#D3D3D3" /> 273 <ControlTemplate.Triggers> 274 <Trigger Property="IsMouseOver" Value="True"> 275 <Setter Property="Fill" TargetName="path" Value="DarkGray" /> 276 <Setter Property="Stroke" TargetName="path" Value="DarkGray" /> 277 </Trigger> 278 <Trigger Property="IsPressed" Value="True"> 279 <Setter Property="Fill" TargetName="path" Value="DarkGray" /> 280 <Setter Property="Stroke" TargetName="path" Value="DarkGray" /> 281 </Trigger> 282 </ControlTemplate.Triggers> 283 </ControlTemplate> 284 </Setter.Value> 285 </Setter> 286 </Style> 287 <!-- 纵向滚动条样式 --> 288 <Style x:Key="ScrollBarStyle" TargetType="{x:Type ScrollBar}"> 289 <Setter Property="Background" Value="Transparent"/> 290 <Setter Property="Template"> 291 <Setter.Value> 292 <ControlTemplate TargetType="{x:Type ScrollBar}"> 293 <Grid x:Name="Bg" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> 294 <Grid.RowDefinitions> 295 <RowDefinition MaxHeight="{DynamicResource {x:Static SystemParameters.VerticalScrollBarButtonHeightKey}}"/> 296 <RowDefinition Height="0.00001*"/> 297 <RowDefinition MaxHeight="{DynamicResource {x:Static SystemParameters.VerticalScrollBarButtonHeightKey}}"/> 298 </Grid.RowDefinitions> 299 <RepeatButton Command="{x:Static ScrollBar.LineUpCommand}" IsEnabled="{TemplateBinding IsMouseOver}" Style="{StaticResource VerticalScrollBarTopButton}" /> 300 <Track x:Name="PART_Track" IsDirectionReversed="true" IsEnabled="{TemplateBinding IsMouseOver}" Grid.Row="1"> 301 <Track.DecreaseRepeatButton> 302 <RepeatButton Command="{x:Static ScrollBar.PageUpCommand}" Style="{StaticResource VerticalScrollBarPageButton}"/> 303 </Track.DecreaseRepeatButton> 304 <Track.IncreaseRepeatButton> 305 <RepeatButton Command="{x:Static ScrollBar.PageDownCommand}" Style="{StaticResource VerticalScrollBarPageButton}"/> 306 </Track.IncreaseRepeatButton> 307 <Track.Thumb> 308 <Thumb Style="{StaticResource VerticalScrollBarThumb}" /> 309 </Track.Thumb> 310 </Track> 311 <RepeatButton Command="{x:Static ScrollBar.LineDownCommand}" IsEnabled="{TemplateBinding IsMouseOver}" Grid.Row="2" Style="{StaticResource VerticalScrollBarButtomButton}" /> 312 </Grid> 313 </ControlTemplate> 314 </Setter.Value> 315 </Setter> 316 <Style.Triggers> 317 <!-- 横向滚动条的样式 --> 318 <Trigger Property="Orientation" Value="Horizontal"> 319 <Setter Property="Background" Value="Transparent"/> 320 <Setter Property="Template"> 321 <Setter.Value> 322 <ControlTemplate TargetType="{x:Type ScrollBar}"> 323 <Grid x:Name="Bg" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> 324 <Grid.ColumnDefinitions> 325 <ColumnDefinition MaxWidth="{DynamicResource {x:Static SystemParameters.HorizontalScrollBarButtonWidthKey}}"/> 326 <ColumnDefinition Width="0.00001*"/> 327 <ColumnDefinition MaxWidth="{DynamicResource {x:Static SystemParameters.HorizontalScrollBarButtonWidthKey}}"/> 328 </Grid.ColumnDefinitions> 329 <RepeatButton Command="{x:Static ScrollBar.LineLeftCommand}" IsEnabled="{TemplateBinding IsMouseOver}" Style="{StaticResource HorizontalScrollBarLeftButton}" /> 330 <Track x:Name="PART_Track" Grid.Column="1" IsEnabled="{TemplateBinding IsMouseOver}"> 331 <Track.DecreaseRepeatButton> 332 <RepeatButton Command="{x:Static ScrollBar.PageLeftCommand}" Style="{StaticResource HorizontalScrollBarPageButton}"/> 333 </Track.DecreaseRepeatButton> 334 <Track.IncreaseRepeatButton> 335 <RepeatButton Command="{x:Static ScrollBar.PageRightCommand}" Style="{StaticResource HorizontalScrollBarPageButton}"/> 336 </Track.IncreaseRepeatButton> 337 <Track.Thumb> 338 <Thumb Style="{StaticResource HorizontalScrollBarThumb}" /> 339 </Track.Thumb> 340 </Track> 341 <RepeatButton Grid.Column="2" Command="{x:Static ScrollBar.LineRightCommand}" IsEnabled="{TemplateBinding IsMouseOver}" Style="{StaticResource HorizontalScrollBarRightButton}" /> 342 </Grid> 343 </ControlTemplate> 344 </Setter.Value> 345 </Setter> 346 </Trigger> 347 </Style.Triggers> 348 </Style>
全是用Blend 拆分做的,删减一些添加一些样式,留作备份!