一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - WPF Slider滑動條的顏色修改方法

WPF Slider滑動條的顏色修改方法

2022-02-27 15:51hbatjzyb C#

這篇文章主要介紹了WPF Slider滑動條的顏色修改方法,需要的朋友可以參考下

效果如下:

WPF Slider滑動條的顏色修改方法

鄙人雖然開發wpf有些時間,但之前一直是一些簡單template和style改改之類的工作,并沒有深入研究過。此次為了完成工作,首先也是網上搜了半天,沒有找到合適的代碼直接拷貝(搜索能力待提高),干脆就直接靜下心來琢磨琢磨。

一開始在界面上就放了slider,撓撓頭,怎么修改template才能達到效果呢?后來想到了blend,之前一直聽說很強大的界面設計工具,但是一直沒有用過,就趁此機會就簡單運用了一下。blend中很牛逼的就是編輯模板,通過創建模板副本,可以看到slider結構

WPF Slider滑動條的顏色修改方法

WPF Slider滑動條的顏色修改方法

結合代碼發現,thumb左右兩邊的reapeatbutton的寬度會隨著thumb的位置會變化。那問題就變得簡單很多,修改左repeatbutton的template就可以達到目的,核心代碼如下。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<style x:key="decreasebtn" targettype="{x:type repeatbutton}">
  <setter property="template">
    <setter.value>
      <controltemplate targettype="{x:type repeatbutton}">
        <border background="{templatebinding background}"
            height="{templatebinding height}" width="{templatebinding width}">
          <!--軌跡,設置background-->
          <border margin="0,0,-1,0" background="{staticresource sliderthumb.track.decreasebackground}"
              verticalalignment="center" height="4.0" />
        </border>
      </controltemplate>
    </setter.value>
  </setter>
</style>

完整代碼(只是考慮水平的slider):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <solidcolorbrush x:key="sliderthumb.static.foreground" color="#ffe5e5e5"/>
  <solidcolorbrush x:key="sliderthumb.mouseover.background" color="gray"/>
  <solidcolorbrush x:key="sliderthumb.mouseover.border" color="#ff7eb4ea"/>
  <solidcolorbrush x:key="sliderthumb.pressed.background" color="gray"/>
  <solidcolorbrush x:key="sliderthumb.pressed.border" color="gray"/>
  <solidcolorbrush x:key="sliderthumb.disabled.background" color="#fff0f0f0"/>
  <solidcolorbrush x:key="sliderthumb.disabled.border" color="#ffd9d9d9"/>
  <solidcolorbrush x:key="sliderthumb.static.background" color="#989898"/>
  <controltemplate x:key="sliderthumbhorizontaltop" targettype="{x:type thumb}">
    <grid horizontalalignment="center" uselayoutrounding="true" verticalalignment="center">
      <path x:name="grip" data="m 0,6 c0,6 5.5,0 5.5,0 5.5,0 11,6 11,6 11,6 11,18 11,18 11,18 0,18 0,18 0,18 0,6 0,6 z"
         fill="{staticresource sliderthumb.static.background}"
         stretch="fill" snapstodevicepixels="true"
         stroke="{binding path=fill, relativesource={x:static relativesource.self}}" strokethickness="1" uselayoutrounding="true" verticalalignment="center"/>
    </grid>
    <controltemplate.triggers>
      <trigger property="ismouseover" value="true">
        <setter property="fill" targetname="grip" value="{staticresource sliderthumb.mouseover.background}"/>
        <setter property="stroke" targetname="grip" value="{staticresource sliderthumb.mouseover.border}"/>
      </trigger>
      <trigger property="isdragging" value="true">
        <setter property="fill" targetname="grip" value="{staticresource sliderthumb.pressed.background}"/>
        <setter property="stroke" targetname="grip" value="{staticresource sliderthumb.pressed.border}"/>
      </trigger>
      <trigger property="isenabled" value="false">
        <setter property="fill" targetname="grip" value="{staticresource sliderthumb.disabled.background}"/>
        <setter property="stroke" targetname="grip" value="{staticresource sliderthumb.disabled.border}"/>
      </trigger>
    </controltemplate.triggers>
  </controltemplate>
  <controltemplate x:key="sliderthumbhorizontalbottom" targettype="{x:type thumb}">
    <grid horizontalalignment="center" uselayoutrounding="true" verticalalignment="center">
      <path x:name="grip" data="m 0,12 c0,12 5.5,18 5.5,18 5.5,18 11,12 11,12 11,12 11,0 11,0 11,0 0,0 0,0 0,0 0,12 0,12 z"
         fill="{staticresource sliderthumb.static.background}"
         stretch="fill"
         snapstodevicepixels="true"
         stroke="{binding path=fill, relativesource={x:static relativesource.self}}" strokethickness="1" uselayoutrounding="true" verticalalignment="center"/>
    </grid>
    <controltemplate.triggers>
      <trigger property="ismouseover" value="true">
        <setter property="fill" targetname="grip" value="{staticresource sliderthumb.mouseover.background}"/>
        <setter property="stroke" targetname="grip" value="{staticresource sliderthumb.mouseover.border}"/>
      </trigger>
      <trigger property="isdragging" value="true">
        <setter property="fill" targetname="grip" value="{staticresource sliderthumb.pressed.background}"/>
        <setter property="stroke" targetname="grip" value="{staticresource sliderthumb.pressed.border}"/>
      </trigger>
      <trigger property="isenabled" value="false">
        <setter property="fill" targetname="grip" value="{staticresource sliderthumb.disabled.background}"/>
        <setter property="stroke" targetname="grip" value="{staticresource sliderthumb.disabled.border}"/>
      </trigger>
    </controltemplate.triggers>
  </controltemplate>
  <solidcolorbrush x:key="sliderthumb.track.background" color="#b6b6b6"/>
  <solidcolorbrush x:key="sliderthumb.track.decreasebackground" color="#45db5e"/>
  <style x:key="repeatbuttontransparent" targettype="{x:type repeatbutton}">
    <setter property="overridesdefaultstyle" value="true"/>
    <setter property="background" value="transparent"/>
    <setter property="focusable" value="false"/>
    <setter property="istabstop" value="false"/>
  </style>
  <style x:key="decreasebtn" targettype="{x:type repeatbutton}" basedon="{staticresource repeatbuttontransparent}">
    <setter property="template">
      <setter.value>
        <controltemplate targettype="{x:type repeatbutton}">
          <border background="{templatebinding background}" height="{templatebinding height}" width="{templatebinding width}">
            <border margin="1,0,-1,0" background="{staticresource sliderthumb.track.decreasebackground}"
                verticalalignment="center" height="4.0" />
          </border>
        </controltemplate>
      </setter.value>
    </setter>
  </style>
  <style x:key="increasebtn" targettype="{x:type repeatbutton}" basedon="{staticresource repeatbuttontransparent}">
    <setter property="template">
      <setter.value>
        <controltemplate targettype="{x:type repeatbutton}">
          <border background="{templatebinding background}" height="{templatebinding height}" width="{templatebinding width}"/>
        </controltemplate>
      </setter.value>
    </setter>
  </style>
  <controltemplate x:key="sliderthumbhorizontaldefault" targettype="{x:type thumb}">
    <grid horizontalalignment="center" uselayoutrounding="true" verticalalignment="center">
      <path x:name="grip" data="m0 512c0 229.230208 229.805588 0 512 0 794.769792 0 1024 229.805588 1024 512 1024 794.769792 794.194412 1024 512 1024 229.230208 1024 0 794.194412 0 512z"
           strokethickness="1" fill="{staticresource sliderthumb.static.background}" stroke="{binding path=fill, relativesource={x:static relativesource.self}}"
           width="18" height="{binding path=width, relativesource={x:static relativesource.self}}"
           stretch="fill" snapstodevicepixels="true" uselayoutrounding="true" verticalalignment="center"/>
    </grid>
    <controltemplate.triggers>
      <trigger property="ismouseover" value="true">
        <setter property="fill" targetname="grip" value="{staticresource sliderthumb.mouseover.background}"/>
      </trigger>
      <trigger property="isdragging" value="true">
        <setter property="fill" targetname="grip" value="{staticresource sliderthumb.pressed.background}"/>
        <setter property="stroke" targetname="grip" value="{staticresource sliderthumb.pressed.border}"/>
      </trigger>
      <trigger property="isenabled" value="false">
        <setter property="fill" targetname="grip" value="{staticresource sliderthumb.disabled.background}"/>
        <setter property="stroke" targetname="grip" value="{staticresource sliderthumb.disabled.border}"/>
      </trigger>
    </controltemplate.triggers>
  </controltemplate>
  <controltemplate x:key="sliderhorizontal" targettype="{x:type slider}">
    <border x:name="border" borderbrush="{templatebinding borderbrush}"
          borderthickness="{templatebinding borderthickness}" background="{templatebinding background}" snapstodevicepixels="true">
      <grid>
        <grid.rowdefinitions>
          <rowdefinition height="15"/>
          <rowdefinition height="auto" minheight="{templatebinding minheight}"/>
          <rowdefinition height="15"/>
        </grid.rowdefinitions>
        <tickbar x:name="toptick" fill="{templatebinding foreground}" height="4" margin="0,0,0,2" placement="top" grid.row="0"
               visibility="collapsed"/>
        <tickbar x:name="bottomtick" fill="{templatebinding foreground}" height="4" margin="0,2,0,0" placement="bottom" grid.row="2"
               visibility="collapsed"/>
        <border x:name="trackbackground" borderbrush="{staticresource sliderthumb.track.background}"
            borderthickness="1" background="{binding path=borderbrush, relativesource={x:static relativesource.self}}"
            height="4.0" margin="5,0" grid.row="1" verticalalignment="center">
          <canvas horizontalalignment="stretch" margin="0,-1">
            <rectangle x:name="part_selectionrange" fill="{dynamicresource {x:static systemcolors.highlightbrushkey}}"
                  height="{binding path=height, elementname=trackbackground}" visibility="hidden"/>
          </canvas>
        </border>
        <track x:name="part_track" grid.row="1">
          <track.decreaserepeatbutton>
            <repeatbutton command="{x:static slider.decreaselarge}" style="{staticresource decreasebtn}"/>
          </track.decreaserepeatbutton>
          <track.increaserepeatbutton>
            <repeatbutton command="{x:static slider.increaselarge}" style="{staticresource increasebtn}"/>
          </track.increaserepeatbutton>
          <track.thumb>
            <thumb x:name="thumb" focusable="false" height="20" overridesdefaultstyle="true"
                template="{staticresource sliderthumbhorizontaldefault}" verticalalignment="center"
                width="{binding path=height, relativesource={x:static relativesource.self}}"/>
          </track.thumb>
        </track>
      </grid>
    </border>
    <controltemplate.triggers>
      <trigger property="tickplacement" value="topleft">
        <setter property="visibility" targetname="toptick" value="visible"/>
        <setter property="template" targetname="thumb" value="{staticresource sliderthumbhorizontaltop}"/>
        <setter property="margin" targetname="trackbackground" value="5,2,5,0"/>
      </trigger>
      <trigger property="tickplacement" value="bottomright">
        <setter property="visibility" targetname="bottomtick" value="visible"/>
        <setter property="template" targetname="thumb" value="{staticresource sliderthumbhorizontalbottom}"/>
        <setter property="margin" targetname="trackbackground" value="5,0,5,2"/>
      </trigger>
      <trigger property="tickplacement" value="both">
        <setter property="visibility" targetname="toptick" value="visible"/>
        <setter property="visibility" targetname="bottomtick" value="visible"/>
      </trigger>
      <trigger property="isselectionrangeenabled" value="true">
        <setter property="visibility" targetname="part_selectionrange" value="visible"/>
      </trigger>
    </controltemplate.triggers>
  </controltemplate>
  <style x:key="sliderstyle" targettype="{x:type slider}">
    <setter property="stylus.ispressandholdenabled" value="false"/>
    <setter property="background" value="transparent"/>
    <setter property="borderbrush" value="transparent"/>
    <setter property="template" value="{staticresource sliderhorizontal}"/>
  </style>
</resourcedictionary>

其實最重要的還是控件的結構,只要對此很熟悉,做出理想的控件應該不難。

總結

以上所述是小編給大家介紹的wpf slider滑動條的顏色修改方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://www.cnblogs.com/hbatjzyb/archive/2018/08/01/9401218.html

延伸 · 閱讀

精彩推薦
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
主站蜘蛛池模板: 四虎精品永久在线网址 | 久久精品国产在热亚洲完整版 | sp啪啪调教打屁股网站 | 色综合亚洲精品激情狠狠 | 国产yw193.㎝m在线观看 | 天堂樱桃bt在线www | 亚洲成人网导航 | 国产自产一区c | 欧美va在线 | 青青草国产免费国产是公开 | 免费精品视频在线 | 美女脱了内裤打开腿让人羞羞软件 | 校花在公车上被内射好舒 | 任我行视频在线观看国语 | 欧美综合影院 | 成人网欧美亚洲影视图片 | 欧美在线播放成人免费 | 奇米影视久久 | 吃大胸寡妇的奶 | 国产婷婷高清在线观看免费 | 精品在线一区 | 97精品国产高清在线看入口 | 摄像头东北对白清晰 | 日本四虎影院 | 精品国产免费观看一区高清 | 欧美精品v欧洲高清 | 国产亚洲精品aaa大片 | 欧美brazzers| 日韩天堂在线 | 逼里逼里香 | 国产精品99在线观看 | 精品国产人成亚洲区 | 日本美女视频韩国视频网站免费 | 亚洲色欲色欲综合网站 | 女人爽到喷水的视频免费 | 日韩毛片高清在线看 | 色老板最新网站视频地址 | 精品免费视在线观看 | 九九365资源稳定资源站 | 日本又大又硬又粗的视频 | 国产精品久久久天天影视香蕉 |