[WPF]关于ListView滚动条样式

avin.wang 2017-06-05 03:57:15
求助,请问怎么修改ListView的滚动条的样式?如下图是默认的样式,感觉好难看...

网上看到一款样式,比较喜欢,代码如下(原链接找不着了,在此对原作者表示感谢!):
<Style x:Key="ScrollBar_style" TargetType="ScrollBar">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ScrollBar">
<Grid Width="15">
<Border Width="13" HorizontalAlignment="Center" CornerRadius="5" Background="#33555555">
</Border>
<Track HorizontalAlignment="Center" Name="PART_Track" Width="{TemplateBinding Width}" Maximum="{TemplateBinding Maximum}" Minimum="{TemplateBinding Minimum}"
Value="{TemplateBinding Value}" IsDirectionReversed="true">
<Track.DecreaseRepeatButton>
<RepeatButton Template="{StaticResource scroll_background}" Command="ScrollBar.LineUpCommand" />
</Track.DecreaseRepeatButton>
<Track.IncreaseRepeatButton>
<RepeatButton Template="{StaticResource scroll_background}" Command="ScrollBar.LineDownCommand" />
</Track.IncreaseRepeatButton>
<Track.Thumb>
<Thumb Style="{StaticResource scroll_thumb_style}" >
</Thumb>
</Track.Thumb>
</Track>

</Grid>

</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

其中:
<Window.Resources>
<ControlTemplate x:Key="scroll_background" TargetType="RepeatButton">
<Border Background="Transparent">
</Border>
</ControlTemplate>

<Style x:Key="scroll_thumb_style" TargetType="Thumb">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Focusable" Value="false"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Thumb">
<Rectangle Width="13" Fill="#7D7D7D" RadiusX="5" RadiusY="5">
</Rectangle>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>

可是,不会用啊,,折腾了好久就是不会用,脑子笨
求大神指点一下,最好给个demo,谢谢了!
...全文
1313 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
avin.wang 2017-06-06
  • 打赏
  • 举报
回复
引用 6 楼 duanzi_peng 的回复:
单修改ScrollViewer 这个控件的style,我是会的,就是不会修改ListView的滚动条。 -》 这里你需要明白,ListView “自带" 滚动条!!! ListVIew 为ItemsControl,任何属于ItemsControl的控件,DataGrid,ListBox等 ,都默认支持ScrollViewer的,不需要特定去修改它。
抱歉才回复!懂啦,非常感谢指点!
  • 打赏
  • 举报
回复
单修改ScrollViewer 这个控件的style,我是会的,就是不会修改ListView的滚动条。 -》 这里你需要明白,ListView “自带" 滚动条!!! ListVIew 为ItemsControl,任何属于ItemsControl的控件,DataGrid,ListBox等 ,都默认支持ScrollViewer的,不需要特定去修改它。
avin.wang 2017-06-05
  • 打赏
  • 举报
回复
@duanzi_peng 就像我帖子里那样,那个ListView的滚动条是ListView控件本身的,不是用scrollviewer来容纳而出现的
avin.wang 2017-06-05
  • 打赏
  • 举报
回复
引用 3 楼 duanzi_peng 的回复:
因为 ScrollViewer 包含ScrollBar,你可以直接用一个ScrollViewer,

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowStartupLocation="CenterScreen"
  Title="" SizeToContent="WidthAndHeight">
  <Window.Resources>
  
    <Style x:Key="scroll_thumb_style" TargetType="{x:Type Thumb}">
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="IsTabStop" Value="false"/>
            <Setter Property="Focusable" Value="false"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Thumb">
                        <Rectangle Width="13" Fill="#7D7D7D" RadiusX="5" RadiusY="5">
                        </Rectangle>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    <ControlTemplate x:Key="scroll_background" TargetType="{x:Type RepeatButton}">
                           <Border Background="Transparent">
                            </Border>
                        </ControlTemplate> 
    <Style TargetType="ScrollBar">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ScrollBar">
                                <Grid Width="15">
                                    <Border Width="13" HorizontalAlignment="Center" CornerRadius="5" Background="#33555555">
                                    </Border>
                                    <Track HorizontalAlignment="Center" Name="PART_Track" Width="{TemplateBinding Width}" Maximum="{TemplateBinding Maximum}" Minimum="{TemplateBinding Minimum}"
                                    Value="{TemplateBinding Value}"  IsDirectionReversed="true">
                                        <Track.DecreaseRepeatButton>
                                            <RepeatButton Template="{StaticResource scroll_background}" Command="ScrollBar.LineUpCommand"  />
                                        </Track.DecreaseRepeatButton>
                                        <Track.IncreaseRepeatButton>
                                            <RepeatButton Template="{StaticResource scroll_background}" Command="ScrollBar.LineDownCommand" />
                                        </Track.IncreaseRepeatButton>
                                        <Track.Thumb>
                                            <Thumb Style="{StaticResource scroll_thumb_style}" >
                                            </Thumb>
                                        </Track.Thumb>
                                    </Track>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
  </Window.Resources>
  <Grid>  
     <ScrollViewer  Height="300">
        <Border Width="500" Height="500" Background="LightBlue"></Border>
     </ScrollViewer>
  </Grid>
</Window>
非常感谢!只是,单修改ScrollViewer 这个控件的style,我是会的,就是不会修改ListView的滚动条。就是我现在有个ListView,当数据比较多的时候会显示滚动条,我是想把现在这个style应用到ListView的滚动条上去,怎么用呢?
  • 打赏
  • 举报
回复
因为 ScrollViewer 包含ScrollBar,你可以直接用一个ScrollViewer,

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowStartupLocation="CenterScreen"
  Title="" SizeToContent="WidthAndHeight">
  <Window.Resources>
  
    <Style x:Key="scroll_thumb_style" TargetType="{x:Type Thumb}">
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="IsTabStop" Value="false"/>
            <Setter Property="Focusable" Value="false"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Thumb">
                        <Rectangle Width="13" Fill="#7D7D7D" RadiusX="5" RadiusY="5">
                        </Rectangle>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    <ControlTemplate x:Key="scroll_background" TargetType="{x:Type RepeatButton}">
                           <Border Background="Transparent">
                            </Border>
                        </ControlTemplate> 
    <Style TargetType="ScrollBar">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ScrollBar">
                                <Grid Width="15">
                                    <Border Width="13" HorizontalAlignment="Center" CornerRadius="5" Background="#33555555">
                                    </Border>
                                    <Track HorizontalAlignment="Center" Name="PART_Track" Width="{TemplateBinding Width}" Maximum="{TemplateBinding Maximum}" Minimum="{TemplateBinding Minimum}"
                                    Value="{TemplateBinding Value}"  IsDirectionReversed="true">
                                        <Track.DecreaseRepeatButton>
                                            <RepeatButton Template="{StaticResource scroll_background}" Command="ScrollBar.LineUpCommand"  />
                                        </Track.DecreaseRepeatButton>
                                        <Track.IncreaseRepeatButton>
                                            <RepeatButton Template="{StaticResource scroll_background}" Command="ScrollBar.LineDownCommand" />
                                        </Track.IncreaseRepeatButton>
                                        <Track.Thumb>
                                            <Thumb Style="{StaticResource scroll_thumb_style}" >
                                            </Thumb>
                                        </Track.Thumb>
                                    </Track>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
  </Window.Resources>
  <Grid>  
     <ScrollViewer  Height="300">
        <Border Width="500" Height="500" Background="LightBlue"></Border>
     </ScrollViewer>
  </Grid>
</Window>
avin.wang 2017-06-05
  • 打赏
  • 举报
回复
哦,单修改滑块的样式不可以么?现在就是想练习下怎么把这个style应用到ListView的滚动条上,具体DIY成啥样暂时不重要
  • 打赏
  • 举报
回复
缺少上下箭头的样式

17,748

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧