WPF自定义控件与样式 - ScrollViewer与ListBox自定义样式

一.前言

  申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接。

本文主要内容:

  • ScrollViewer的样式拆解及基本样式定义;
  • ListBox集合控件的样式定义;

二.ScrollViewer自定义样式

ScrollViewer在各种列表、集合控件中广泛使用的基础组建,先看看效果图:

如上图,ScrollViewer简单来说分两部分,一个横向的滚动条,一个垂直滚动条,两个样式、模板、功能都基本一样,他们都是ScrollBar。以垂直滚动条为例,分解一下,分解图:

 

  • 1:向上滑动的按钮,用RepeatButton实现功能;
  • 2:上部分滑块,功能同1,也是一个RepeatButton来实现的;
  • 3:中间可拖动滑块,用一个Thumb来实现;
  • 4:下部分滑块,和5功能一样,向下滑动,用一个RepeatButton来实现;
  • 5:向下滑动的按钮,用RepeatButton实现功能;

  上面实现的是一个标准的垂直滑动条ScrollBar组成,实际可用根据需求定制,实现不同效果的滑动效果。以上各部分的样式代码:

 

<sys:Double x:Key="ScrollBarSize">12</sys:Double>
    <!--滚动两边按钮样式-->
    <Style x:Key="ScrollBarButton" TargetType="{x:Type RepeatButton}">
        <Setter Property="Background" Value="Transparent"></Setter>
        <Setter Property="Foreground" Value="{StaticResource TextForeground}"></Setter>
        <Setter Property="VerticalAlignment" Value="Center"></Setter>
        <Setter Property="HorizontalAlignment" Value="Center"></Setter>
        <Setter Property="Width" Value="auto"></Setter>
        <Setter Property="Height" Value="auto"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RepeatButton}">
                    <TextBlock x:Name="FIcon" FontSize="12" Text="{TemplateBinding local:ControlAttachProperty.FIcon}" Margin="1"
                               Style="{StaticResource FIcon}" />
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="{StaticResource MouseOverForeground}" TargetName="FIcon"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Foreground" Value="{StaticResource PressedForeground}" TargetName="FIcon"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Opacity" Value="0.5" TargetName="FIcon"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!--滚动条滑块两边按钮样式-->
    <Style x:Key="ScrollBarTrackButton" TargetType="{x:Type RepeatButton}">
        <Setter Property="Background" Value="Transparent"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RepeatButton}">
                    <Border Background="Transparent"></Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!--滚动条滑块样式-->
    <ControlTemplate x:Key="ThumbTemplate" TargetType="Thumb">
        <Grid>
            <Border  x:Name="Bg" CornerRadius="4" Margin="2" SnapsToDevicePixels="True" Background="{StaticResource ScrollBarForeround}">
                <!--<Border.Background>
                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                        <GradientStop Color="#C7C0C0" Offset="0.15"/>
                        <GradientStop Color="#AFA9A9" Offset=".5"/>
                        <GradientStop Color="#989494" Offset=".5"/>
                        <GradientStop Color="#858585" Offset="1"/>
                    </LinearGradientBrush>
                </Border.Background>-->
            </Border>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="{StaticResource MouseOverForeground}" TargetName="Bg"></Setter>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" Value="0.5" TargetName="Bg"></Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!--水平滚滚动条模板-->
    <ControlTemplate x:Key="HorizontalScrollBar" TargetType="{x:Type ScrollBar}">
        <Grid x:Name="HorizontalRoot" Height="{TemplateBinding Height}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <!--外部背景,好像不用更好看-->
            <!--<Border x:Name="Bg" Grid.Column="0" Grid.ColumnSpan="3" CornerRadius="0"  Opacity="0" Background="#858585"/>-->
            <!--内部背景-->
            <Border x:Name="BgInner" Grid.Column="1" Margin="0" SnapsToDevicePixels="True" Opacity="0.3"  CornerRadius="6" Background="{StaticResource ScrollBarBackground}"/>
            <!--左按钮-->

            <Border Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center">
                <RepeatButton  local:ControlAttachProperty.FIcon=""  Style="{StaticResource ScrollBarButton}" x:Name="HorizontalSmallDecrease"
                                 IsTabStop="False" Interval="50" Margin="0,1,0,0" Command="ScrollBar.LineLeftCommand"/>
            </Border>
            <!--中间滑动区域-->
            <Track x:Name="PART_Track" IsDirectionReversed="False" Grid.Column="1">
                <!--左滑块-->
                <Track.DecreaseRepeatButton>
                    <RepeatButton x:Name="HorizontalLargeDecrease" Command="ScrollBar.PageLeftCommand"
                                      IsTabStop="False" Interval="50" Style="{DynamicResource ScrollBarTrackButton}" />
                </Track.DecreaseRepeatButton>
                <!--中间滑块 Margin="1" VerticalAlignment="Center" VerticalContentAlignment="Center" -->
                <Track.Thumb>
                    <Thumb Template="{StaticResource ThumbTemplate}" />
                </Track.Thumb>
                <!--右滑块-->
                <Track.IncreaseRepeatButton>
                    <RepeatButton x:Name="HorizontalLargeIncrease" Command="ScrollBar.PageRightCommand"
                                      IsTabStop="False"  Interval="50" Style="{DynamicResource ScrollBarTrackButton}" />
                </Track.IncreaseRepeatButton>
            </Track>
            <!--右按钮-->
            <Border Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center">
                <RepeatButton local:ControlAttachProperty.FIcon=""  Style="{StaticResource ScrollBarButton}"
                                 IsTabStop="False" Interval="50" Margin="0,1,0,0" Command="ScrollBar.LineRightCommand"/>
            </Border>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter TargetName="BgInner" Property="Opacity" Value="0.5"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!--垂直滚滚动条模板-->
    <ControlTemplate x:Key="VerticalScrollBar" TargetType="{x:Type ScrollBar}">
        <Grid x:Name="VerticalRoot" Height="{TemplateBinding Height}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <!--外部背景,好像不用更好看-->
            <!--<Border x:Name="Bg" Grid.Row="0" Grid.RowSpan="3" CornerRadius="0" Opacity="0" Background="#858585"/>-->
            <!--内部背景-->
            <Border x:Name="BgInner" Grid.Row="1" Margin="0" CornerRadius="6" SnapsToDevicePixels ="True" Opacity="0.3"  Background="{StaticResource ScrollBarBackground}"/>
            <!--上按钮-->
            <Border Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="VerticalSmallDecrease">
                <RepeatButton local:ControlAttachProperty.FIcon=""  Style="{StaticResource ScrollBarButton}"
                                 IsTabStop="False" Interval="50" Margin="0" Command="ScrollBar.LineUpCommand"/>
            </Border>
            <!--中间滑动区域-->
            <Track x:Name="PART_Track" IsDirectionReversed="true" Grid.Row="1">
                <!--上滑块-->
                <Track.DecreaseRepeatButton>
                    <RepeatButton x:Name="HorizontalLargeDecrease" Command="ScrollBar.PageUpCommand" 
                                      IsTabStop="False" Interval="50" Style="{DynamicResource ScrollBarTrackButton}" />
                </Track.DecreaseRepeatButton>
                <!--中间滑块-->
                <Track.Thumb>
                    <Thumb Template="{StaticResource ThumbTemplate}" MinHeight="10"/>
                </Track.Thumb>
                <!--下滑块-->
                <Track.IncreaseRepeatButton>
                    <RepeatButton x:Name="HorizontalLargeIncrease" Command="ScrollBar.PageDownCommand"
                                      IsTabStop="False" Interval="50" Style="{DynamicResource ScrollBarTrackButton}" />
                </Track.IncreaseRepeatButton>
            </Track>
            <!--下按钮-->
            <Border Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="VerticalSmallIncrease">
                <RepeatButton local:ControlAttachProperty.FIcon=""  Style="{StaticResource ScrollBarButton}"
                                 IsTabStop="False" Interval="50" Margin="0" Command="ScrollBar.LineDownCommand"/>
            </Border>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter TargetName="BgInner" Property="Opacity" Value="0.5"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!--ScrollBar样式-->
    <Style x:Key="DefaultScrollBar" TargetType="{x:Type ScrollBar}">
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Style.Triggers>
            <Trigger Property="Orientation" Value="Horizontal">
                <Setter Property="Template" Value="{StaticResource HorizontalScrollBar}" />
                <Setter Property="Height" Value="{StaticResource ScrollBarSize}" />
            </Trigger>
            <Trigger Property="Orientation" Value="Vertical">
                <Setter Property="Template" Value="{StaticResource VerticalScrollBar}" />
                <Setter Property="Width" Value="{StaticResource ScrollBarSize}" />
            </Trigger>
        </Style.Triggers>
    </Style>

 

  

最后ScrollViewer的样式如下,其实就是两个  ScrollBar组成:

 

 1  <!--ScrollViewer样式-->
 2     <Style x:Key="DefaultScrollViewer" TargetType="{x:Type ScrollViewer}">
 3         <Setter Property="Template">
 4             <Setter.Value>
 5                 <ControlTemplate TargetType="{x:Type ScrollViewer}">
 6                     <Grid x:Name="Grid" Background="{TemplateBinding Background}">
 7                         <Grid.ColumnDefinitions>
 8                             <ColumnDefinition Width="*" x:Name="leftColumn" />
 9                             <ColumnDefinition Width="Auto" x:Name="rightColumn" />
10                         </Grid.ColumnDefinitions>
11                         <Grid.RowDefinitions>
12                             <RowDefinition Height="*" />
13                             <RowDefinition Height="Auto" />
14                         </Grid.RowDefinitions>
15                         <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}"
16                                                 CanHorizontallyScroll="False" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}"
17                                                 Content="{TemplateBinding Content}" Margin="{TemplateBinding Padding}"
18                                                 Grid.Row="0" Grid.Column="0" />
19                         <!--垂直滚动条 -->
20                         <ScrollBar x:Name="PART_VerticalScrollBar" AutomationProperties.AutomationId="VerticalScrollBar"
21                                    ViewportSize="{TemplateBinding ViewportHeight}"
22                                    Cursor="Arrow" Grid.Column="1" Maximum="{TemplateBinding ScrollableHeight}"
23                                    Minimum="0" Grid.Row="0" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
24                                    Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/>
25                         <!--水平底部滚动条-->
26                         <ScrollBar x:Name="PART_HorizontalScrollBar" AutomationProperties.AutomationId="HorizontalScrollBar"
27                                    Cursor="Arrow" Grid.Column="0" Maximum="{TemplateBinding ScrollableWidth}"
28                                    Minimum="0" Orientation="Horizontal" Grid.Row="1"
29                                    Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
30                                    Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
31                                    ViewportSize="{TemplateBinding ViewportWidth}" />
32                     </Grid>
33                 </ControlTemplate>
34             </Setter.Value>
35         </Setter>
36     </Style>

 

使用很简单,如果想通用,把上面定义的ScrollViewer设置为默认样式即可:

<Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource DefaultScrollBar}"></Style>

<Style TargetType="{x:Type ScrollViewer}" BasedOn="{StaticResource DefaultScrollViewer}"></Style>

 

 

三.ListBox样式定义

 

ListBox是最基础、常用的集合控件,效果:

 

 

ListBox的样式比较简单,包括两部分:

 

  • ListBoxItem项的样式;
  • ListBox的样式;

 

完整代码:

 

 

 1 <Style x:Key="DefaultListBoxItem" TargetType="{x:Type ListBoxItem}">
 2         <Setter Property="Foreground" Value="{StaticResource TextForeground}" />
 3         <Setter Property="HorizontalContentAlignment" Value="Stretch" />
 4         <!--<Setter Property="VerticalContentAlignment" Value="Center" />-->
 5         <Setter Property="MinHeight" Value="25" />
 6         <Setter Property="Margin" Value="0" />
 7         <Setter Property="Background" Value="Transparent" />
 8         <Setter Property="Padding" Value="3,0,0,0" />
 9         <Setter Property="SnapsToDevicePixels" Value="True" />
10         <Setter Property="Template">
11             <Setter.Value>
12                 <ControlTemplate TargetType="{x:Type ListBoxItem}">
13                     <Border x:Name="Border" Background="{TemplateBinding Background}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
14                         <ContentPresenter Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
15                                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
16                     </Border>
17                     <ControlTemplate.Triggers>
18                         <Trigger Property="IsSelected" Value="True">
19                             <Setter TargetName="Border" Property="Background" Value="{StaticResource ItemSelectedBackground}" />
20                             <Setter Property="Foreground" Value="{StaticResource ItemSelectedForeground}" />
21                         </Trigger>
22                         <Trigger Property="IsMouseOver" Value="True">
23                             <Setter TargetName="Border" Property="Background" Value="{StaticResource ItemMouseOverBackground}" />
24                             <Setter Property="Foreground" Value="{StaticResource ItemMouseOverForeground}" />
25                         </Trigger>
26                         <Trigger Property="IsEnabled" Value="False">
27                             <Setter TargetName="Border" Property="Opacity" Value="{StaticResource DisableOpacity}" />
28                         </Trigger>
29                     </ControlTemplate.Triggers>
30                 </ControlTemplate>
31             </Setter.Value>
32         </Setter>
33     </Style>

 

 

 1 <Style x:Key="DefaultListBox" TargetType="{x:Type ListBox}">
 2         <Setter Property="BorderBrush" Value="{StaticResource ControlBorderBrush}" />
 3         <Setter Property="Background" Value="{StaticResource ItemsContentBackground}" />
 4         <Setter Property="BorderThickness" Value="1" />
 5         <Setter Property="ItemContainerStyle" Value="{StaticResource DefaultListBoxItem}"></Setter>
 6         <Setter Property="SnapsToDevicePixels" Value="True" />
 7         <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="False"></Setter>
 8         <Setter Property="Template">
 9             <Setter.Value>
10                 <ControlTemplate TargetType="{x:Type ListBox}">
11                     <Border Name="Border" Background="{TemplateBinding Background}"
12                             BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
13                         <ScrollViewer>
14                             <ItemsPresenter />
15                         </ScrollViewer>
16                     </Border>
17                     <ControlTemplate.Triggers>
18                         <Trigger Property="IsEnabled" Value="False">
19                             <Setter TargetName="Border" Property="Opacity" Value="{StaticResource DisableOpacity}" />
20                         </Trigger>
21                     </ControlTemplate.Triggers>
22                 </ControlTemplate>
23             </Setter.Value>
24         </Setter>
25     </Style>

ListBox默认是支持虚拟化的,当加载大数据时需要开启虚拟化,或者定义一个虚拟化的样式:

 

 

1  <!--支持虚拟化的ListBox-->
2     <Style x:Key="VirtualListBox" TargetType="{x:Type ListBox}" BasedOn="{StaticResource DefaultListBox}">
3         <Setter Property="ScrollViewer.CanContentScroll" Value="True" />
4         <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"></Setter>
5         <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling" />
6         <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False" />
7     </Style>

 

上面演示效果的代码示例: 

 

 1 <ListBox Margin="5" SelectionMode="Multiple"  >
 2                 <ListBoxItem  Style="{StaticResource RadioButtonListBoxItem}">男</ListBoxItem>
 3                 <ListBoxItem  Style="{StaticResource RadioButtonListBoxItem}">女</ListBoxItem>
 4                 <ListBoxItem  Style="{StaticResource RadioButtonListBoxItem}">其他</ListBoxItem>
 5                 <CheckBox>2222333333333333333</CheckBox>
 6                 <CheckBox>2222333333333333333</CheckBox>
 7                 <CheckBox>2222333333333333333</CheckBox>
 8                 <CheckBox>2222333333333333333</CheckBox>
 9                 <CheckBox>2222333333333333333</CheckBox>
10                 <CheckBox>2222333333333333333</CheckBox>
11                 <CheckBox>2222333333333333333</CheckBox>
12                 <CheckBox>2222333333333333333</CheckBox>
13                 <TextBox Width="200"></TextBox>
14                 <ListBoxItem IsSelected="True">111</ListBoxItem>
15             </ListBox>
16             <ListBox Margin="5" Grid.Column="1">
17                 <ListBoxItem  Style="{StaticResource RadioButtonListBoxItem}">男</ListBoxItem>
18                 <ListBoxItem  Style="{StaticResource RadioButtonListBoxItem}">女</ListBoxItem>
19                 <ListBoxItem  Style="{StaticResource RadioButtonListBoxItem}">其他</ListBoxItem>
20             </ListBox>

 

另外提供一个比较常用的需求,ListBox单选RadioButton效果样式,如上图右边的那个ListBox效果: 

 

 1  <Style x:Key="RadioButtonListBoxItem" TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource DefaultListBoxItem}">
 2         <Setter Property="Template">
 3             <Setter.Value>
 4                 <ControlTemplate TargetType="{x:Type ListBoxItem}">
 5                     <Border x:Name="Border" Background="{TemplateBinding Background}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
 6                         <RadioButton  IsChecked="{Binding IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}">
 7                             <RadioButton.Content>
 8                                 <ContentPresenter Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
 9                                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
10                             </RadioButton.Content>
11                         </RadioButton>
12                     </Border>
13                     <ControlTemplate.Triggers>
14                         <Trigger Property="IsSelected" Value="True">
15                             <Setter TargetName="Border" Property="Background" Value="{StaticResource ItemSelectedBackground}" />
16                             <Setter Property="Foreground" Value="{StaticResource ItemSelectedForeground}" />
17                         </Trigger>
18                         <Trigger Property="IsMouseOver" Value="True">
19                             <Setter TargetName="Border" Property="Background" Value="{StaticResource ItemMouseOverBackground}" />
20                             <Setter Property="Foreground" Value="{StaticResource ItemMouseOverForeground}" />
21                         </Trigger>
22                         <Trigger Property="IsEnabled" Value="False">
23                             <Setter TargetName="Border" Property="Opacity" Value="{StaticResource DisableOpacity}" />
24                         </Trigger>
25                     </ControlTemplate.Triggers>
26                 </ControlTemplate>
27             </Setter.Value>
28         </Setter>
29     </Style>

 

 

版权所有,文章来源:http://www.cnblogs.com/anding

个人能力有限,本文内容仅供学习、探讨,欢迎指正、交流。

 

 

 

 

 

 

 

 

posted @ 2016-05-18 18:00  Demo无言  阅读(237)  评论(0)    收藏  举报