WPF如何自定义ProgressBar滚动条样式
程序员文章站
2023-09-10 10:07:48
一、前言
滚动条一般用于加载进度,我们在看视频的时候或者在浏览网页的时候经常能看到加载进度的页面。在程序开发中,默认的进度加载样式可能跟程序风格不太一样,或者加载进度...
一、前言
滚动条一般用于加载进度,我们在看视频的时候或者在浏览网页的时候经常能看到加载进度的页面。在程序开发中,默认的进度加载样式可能跟程序风格不太一样,或者加载进度的时候需要更改一下加载的样式。这个时候就需要通过修改progressbar的样式来实现。
二、progressbar的基本样式
progressbar的基本样式很简单:
<style targettype="{x:type progressbar}"> <setter property="focusvisualstyle" value="{x:null}"/> <setter property="snapstodevicepixels" value="true"/> <setter property="height" value="15"/> <setter property="background" value="#6fae5f"/> <setter property="fontsize" value="10"/> <setter property="padding" value="5,0"/> <setter property="template"> <setter.value> <controltemplate targettype="{x:type progressbar}"> <grid background="#00000000"> <grid.rowdefinitions> <rowdefinition height="auto"/> </grid.rowdefinitions> <visualstatemanager.visualstategroups> <visualstategroup x:name="commonstates"> <visualstate x:name="determinate"/> <visualstate x:name="indeterminate"> <storyboard repeatbehavior="forever"> <pointanimationusingkeyframes storyboard.targetname="animation" storyboard.targetproperty="(uielement.rendertransformorigin)"> <easingpointkeyframe keytime="0:0:0" value="0.5,0.5"/> <easingpointkeyframe keytime="0:0:1.5" value="1.95,0.5"/> <easingpointkeyframe keytime="0:0:3" value="0.5,0.5"/> </pointanimationusingkeyframes> </storyboard> </visualstate> </visualstategroup> </visualstatemanager.visualstategroups> <grid height="{templatebinding height}"> <border background="#000000" cornerradius="7.5" opacity="0.05"/> <border borderbrush="#000000" borderthickness="1" cornerradius="7.5" opacity="0.1"/> <grid margin="{templatebinding borderthickness}"> <border x:name="part_track"/> <grid x:name="part_indicator" cliptobounds="true" horizontalalignment="left" > <grid.columndefinitions> <columndefinition x:name="width1"/> <columndefinition x:name="width2" width="0"/> </grid.columndefinitions> <grid x:name="animation" rendertransformorigin="0.5,0.5"> <grid.rendertransform> <transformgroup> <scaletransform scaley="-1" scalex="1"/> <skewtransform angley="0" anglex="0"/> <rotatetransform angle="180"/> <translatetransform/> </transformgroup> </grid.rendertransform> <border background="{templatebinding background}" cornerradius="7.5"> <viewbox horizontalalignment="left" stretchdirection="downonly" margin="{templatebinding padding}" snapstodevicepixels="true"> <textblock foreground="#ffffff" snapstodevicepixels="true" fontsize="{templatebinding fontsize}" verticalalignment="center" text="{binding relativesource={relativesource templatedparent},path=value,stringformat={}{0}%}" rendertransformorigin="0.5,0.5"> <textblock.rendertransform> <transformgroup> <scaletransform scaley="1" scalex="-1"/> <skewtransform angley="0" anglex="0"/> <rotatetransform angle="0"/> <translatetransform/> </transformgroup> </textblock.rendertransform> </textblock> </viewbox> </border> <border borderbrush="#000000" borderthickness="1" cornerradius="7.5" opacity="0.1"/> </grid> </grid> </grid> </grid> </grid> <controltemplate.triggers> <trigger property="isenabled" value="false"> <setter property="background" value="#c5c5c5"/> </trigger> <trigger property="isindeterminate" value="true"> <setter targetname="width1" property="width" value="0.25*"/> <setter targetname="width2" property="width" value="0.725*"/> </trigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style>
引用示例:
<progressbar height="15" width="150" value="40" margin="10"/>
显示效果:
所有代码已经上传到github:https://github.com/cmfgit/wpfdemo.git
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。