wpf之自定义滚动条(代码片段)

lonelyxmas lonelyxmas     2023-01-09     271

关键词:

原文:wpf之自定义滚动条

首先我们添加一个带滚动条的textbox控件:

<ScrollViewer Height="130" Width="620" VerticalScrollBarVisibility="Auto" Style="StaticResource for_scrollviewer">
<TextBlock xml:space="preserve" Name="FtpServerConf" HorizontalAlignment="Left" VerticalAlignment="Top" TextWrapping="Wrap"  Width="610"/>
</ScrollViewer>

这里 VerticalScrollBarVisibility="Auto"表示是超出垂直距离自动显示滚动条,当然这个滚动条是win系统自带的效果,我们要修改的就是这部分,需要对滚动区域的模板进行自定义修改。

这里借用网上的两张图,解释一下滚动条的结构:

技术分享图片技术分享图片

我们需要给scrollview控件自定义样式:

技术分享图片
<Style x:Key="for_scrollviewer"  
           TargetType="x:Type ScrollViewer">
        <Setter Property="BorderBrush"  
                Value="LightGray"/>
        <Setter Property="BorderThickness"  
                Value="0"/>
        <Setter Property="HorizontalContentAlignment"  
                Value="Left"/>
        <Setter Property="HorizontalScrollBarVisibility"  
                Value="Disabled"/>
        <Setter Property="VerticalContentAlignment"  
                Value="Top"/>
        <Setter Property="VerticalScrollBarVisibility"  
                Value="Visible"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="x:Type ScrollViewer">
                    <Border BorderBrush="TemplateBinding BorderBrush"  
                            BorderThickness="TemplateBinding BorderThickness"  
                            SnapsToDevicePixels="True">
                        <Grid Background="TemplateBinding Background">
                            <ScrollContentPresenter  
                                Cursor="TemplateBinding Cursor"  
                                Margin="TemplateBinding Padding"  
                                ContentTemplate="TemplateBinding ContentTemplate"/>
                            <!--垂直滚动条-->
                            <ScrollBar x:Name="PART_VerticalScrollBar"  
                                       HorizontalAlignment="Right"  
                                       Maximum="TemplateBinding ScrollableHeight"  
                                       Orientation="Vertical"  
                                       Style="StaticResource for_scrollbar"  
                                       ViewportSize="TemplateBinding ViewportHeight"  
                                       Value="TemplateBinding VerticalOffset"  
                                       Visibility="TemplateBinding ComputedVerticalScrollBarVisibility"/>
                            <!--水平滚动条-->
                            <ScrollBar x:Name="PART_HorizontalScrollBar"  
                                       Maximum="TemplateBinding ScrollableWidth"  
                                       Orientation="Horizontal"  
                                       Style="StaticResource for_scrollbar"  
                                       VerticalAlignment="Bottom"  
                                       Value="TemplateBinding HorizontalOffset"  
                                       ViewportSize="TemplateBinding ViewportWidth"  
                                       Visibility="TemplateBinding ComputedHorizontalScrollBarVisibility"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="ScrollChanged">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation  
                                        Storyboard.TargetName="PART_VerticalScrollBar"  
                                        Storyboard.TargetProperty="Opacity"  
                                        To="1"  
                                        Duration="0:0:1"/>
                                    <!--自动消失-->
                                    <!--<DoubleAnimation  
                                        Storyboard.TargetName="PART_VerticalScrollBar"  
                                        Storyboard.TargetProperty="Opacity"  
                                        To="0"  
                                        Duration="0:0:1"  
                                        BeginTime="0:0:1"/>-->
                                    <DoubleAnimation  
                                        Storyboard.TargetName="PART_HorizontalScrollBar"  
                                        Storyboard.TargetProperty="Opacity"  
                                        To="1"  
                                        Duration="0:0:1"/>
                                    <DoubleAnimation  
                                        Storyboard.TargetName="PART_HorizontalScrollBar"  
                                        Storyboard.TargetProperty="Opacity"  
                                        To="0"  
                                        Duration="0:0:1"  
                                        BeginTime="0:0:1"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="MouseEnter"  
                                      SourceName="PART_VerticalScrollBar">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation  
                                        Storyboard.TargetName="PART_VerticalScrollBar"  
                                        Storyboard.TargetProperty="Opacity"  
                                        To="1"  
                                        Duration="0:0:1"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <!---鼠标离开后渐渐消失-->
                        <!--
                        <EventTrigger RoutedEvent="MouseLeave"  
                                      SourceName="PART_VerticalScrollBar">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation  
                                        Storyboard.TargetName="PART_VerticalScrollBar"  
                                        Storyboard.TargetProperty="Opacity"  
                                        To="0"  
                                        Duration="0:0:1"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        -->
                        <EventTrigger RoutedEvent="MouseEnter"  
                                      SourceName="PART_HorizontalScrollBar">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation  
                                        Storyboard.TargetName="PART_HorizontalScrollBar"  
                                        Storyboard.TargetProperty="Opacity"  
                                        To="1"  
                                        Duration="0:0:1"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="MouseLeave"  
                                      SourceName="PART_HorizontalScrollBar">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation  
                                        Storyboard.TargetName="PART_HorizontalScrollBar"  
                                        Storyboard.TargetProperty="Opacity"  
                                        To="0"  
                                        Duration="0:0:1"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
View Code

然后在需要使用的地方进行引用:

<ScrollViewer Height="130" Width="620" VerticalScrollBarVisibility="Auto" Style="StaticResource for_scrollviewer">
<TextBlock xml:space="preserve" Name="FtpServerConf" HorizontalAlignment="Left" VerticalAlignment="Top" TextWrapping="Wrap" Style="StaticResource tab_text" Width="610"></TextBlock>
</ScrollViewer>

最终效果是这样的:

技术分享图片





wpf编程之自定义button控件样式(代码片段)

  自.NETFramework3.0以后,WPF编程框架可使开发人员开发出更加令人耳目一新的桌面应用程序。它使开发工作更加方便快捷,它将设计人员和编程人员的工作分离开来。至于WPF的背景历史、框架特点、框架结构这里就不再赘述。... 查看详情

wpf自定义控件之仿win8滚动条--scrollviewer(代码片段)

原文:WPF自定义控件之仿Win8滚动条--ScrollViewer1.说明       自己学习WPF不是很久,现将自己做的一些小项目中用到的自定义控件整理出来,方便以后使用,不尽人意之处,还请多多批评与指导,现在就来实... 查看详情

wpf滚动条处在滚动状态(代码片段)

 显示滚动状态ProgressBarIsIndeterminate="True"Task中操作界面需要用到界面委托Application.Current.Dispatcher.Invoke(()=>);wpf窗口中心显示WindowStartupLocation="CenterScreen" 查看详情

wpf中让滚动条滚动到指定元素位置(代码片段)

///<summary>///垂直方向滚动到顶部///</summary>///<paramname="element"></param>///<paramname="scrollViewer"></param>publicstaticvoidScrollViewToVerticalTop(FrameworkElementel 查看详情

自定义滚动条(customscrollbar)(代码片段)

时间如流水,只能流去不流回!点赞再看,养成习惯,这是您给我创作的动力!本文Dotnet9https://dotnet9.com已收录,站长乐于分享dotnet相关技术,比如Winform、WPF、ASP.NETCore等,亦有C++桌面相关的QtQuick和QtWidgets等,只分享自己熟悉的... 查看详情

wpf走马灯文字滚动自定义控件(代码片段)

原文:WPF走马灯文字滚动自定义控件///<summary>///Label走马灯自定义控件///</summary>[ToolboxBitmap(typeof(Label))]//设置工具箱中显示的图标publicclassScrollingTextControl:Label///<summary>///定时器///</summary>Timer 查看详情

wpf自学入门wpf路由事件之自定义路由事件

    在上一篇博文中写到了内置路由事件,其实除了内置的路由事件,我们也可以进行自定义路由事件。接下来我们一起来看一下WPF中的自定义路由事件怎么进行创建吧。创建自定义路由事件分为3个步骤:1、声明... 查看详情

2019-11-29-wpf-开启-scrollviewer-的触摸滚动(代码片段)

原文:2019-11-29-WPF-开启-ScrollViewer-的触摸滚动titleauthordateCreateTimecategoriesWPF开启ScrollViewer的触摸滚动lindexi2019-11-2910:21:47+08002018-12-2611:51:31+0800WPF在ScrollViewer如果需要收到触摸消息,通过Manipulation触摸滚动,不能只是通过设置IsManipulat... 查看详情

markdown自定义滚动条(代码片段)

查看详情

css自定义滚动条(代码片段)

查看详情

css自定义滚动条(代码片段)

查看详情

css自定义滚动条(代码片段)

查看详情

自定义滚动条mcustomscrollbar(代码片段)

....com/alantao/p/5239262.html mCustomScrollbar是个基于jQueryUI的自定义滚动条插件,它可以让你灵活的通过CSS定义网页的滚动条,并且垂直和水平两个方向的滚动条都可以定义,它通过BrandonAaronjquerymouse-wheelplugin提供了鼠标滚动的支持,... 查看详情

scss自定义默认滚动条(代码片段)

查看详情

css自定义webkit滚动条(代码片段)

查看详情

flutter实战自定义滚动条(代码片段)

老孟导读:【Flutter实战】系列文章地址:http://laomengit.com/guide/introduction/mobile_system.html默认情况下,Flutter的滚动组件(比如ListView)没有显示滚动条,使用Scrollbar显示滚动条:Scrollbar(child:ListView.builder(reverse:false,itemBuilder:(Buil 查看详情

css自定义滚动条(仅限webkit)(代码片段)

查看详情

wpf自定义控件の自定义控件(代码片段)

原文:WPF自定义控件(四)の自定义控件在实际工作中,WPF提供的控件并不能完全满足不同的设计需求。这时,需要我们设计自定义控件。这里LZ总结一些自己的思路,特性如下:CouplingUITemplateBehaviourFunctionPackage下面举例说说在项... 查看详情