自定義乙個WPF的PathButton

2022-01-17 00:31:50 字數 3392 閱讀 3151

一、背景

二、實現

以設定按鈕為例,當然這裡的顏色可能和你需要的不一樣,但是沒關係是可以自定義的

思路是寫乙個usercontrol,繼承自button,然會應用乙個樣式,這裡不直接用樣式而是用usercontrol是因為加了幾個dependencyproperty,這樣就可以自定義顏色了

xaml**

1

"pathbutton.pathbutton

"2 xmlns="

"3 xmlns:x="

"4 xmlns:d="

"5 xmlns:mc="

"6 style="

"7 d:designheight="

300"

8 d:designwidth="

300"

9 mc:ignorable="

d">

10112467

6869

後台**

1

public

partial

class

pathbutton : button26

set7}8

9//using a dependencyproperty as the backing store for pathdata. this enables animation, styling, binding, etc...

10public

static

readonly dependencyproperty pathdataproperty =

11 dependencyproperty.register("

pathdata

", typeof(geometry), typeof(pathbutton), new propertymetadata(new

pathgeometry()));

1213

public

brush defaultfillbrush

1416

set17}18

19//

using a dependencyproperty as the backing store for defaultfillbrush. this enables animation, styling, binding, etc...

20public

static

readonly dependencyproperty defaultfillbrushproperty =

21 dependencyproperty.register("

defaultfillbrush

", typeof(brush), typeof(pathbutton), new

propertymetadata(brushes.darkgray));

2223

public

brush mouseoverbrush

2426

set27}28

29//

using a dependencyproperty as the backing store for mouseoverbrush. this enables animation, styling, binding, etc...

30public

static

readonly dependencyproperty mouseoverbrushproperty =

31 dependencyproperty.register("

mouseoverbrush

", typeof(brush), typeof(pathbutton), new

propertymetadata(brushes.deepskyblue));

3233

public

brush ispressedbrush

3436

set37}38

39//

using a dependencyproperty as the backing store for ispressedbrush. this enables animation, styling, binding, etc...

40public

static

readonly dependencyproperty ispressedbrushproperty =

41 dependencyproperty.register("

ispressedbrush

", typeof(brush), typeof(pathbutton), new

propertymetadata(brushes.dodgerblue));

4243

public

brush isenabledbrush

4446

set47}48

49//

using a dependencyproperty as the backing store for isenabledbrush. this enables animation, styling, binding, etc...

50public

static

readonly dependencyproperty isenabledbrushproperty =

51 dependencyproperty.register("

isenabledbrush

", typeof(brush), typeof(pathbutton), new

propertymetadata(brushes.lightgray));

5253

public

pathbutton()

5458 }

用法  

這樣就完成了,你只需要給乙個你path就可以顯示你想要的按鈕了,如果想改變顏色就設定下3個brush就可以

三、效果

預設樣式

滑鼠進入

滑鼠按下

tips:可以在 nuget 裡搜尋 pathbutton 安裝使用啦

自定義乙個死鎖

在鎖o1釋放之前需要等待鎖o2的釋放 在鎖o2釋放之前需要等待鎖o1的釋放.死在無限等待對方釋放鎖的迴圈中.這只是乙個簡單的死鎖 a需要b,b需要a 複雜一些的有 a需要b,b需要c,c需要a.public class demo6 implements runnable catch interrup...

自定義乙個TagLayout

自定義乙個標籤自動排版顯示的布局。效果 思路 一 確定布局的寬,高。寬度確定 所有行中,寬度最寬的行即為這個布局的寬。高度確定 所有行的高度加起來 即 為這個布局的高度。二 擺放子view思路 如果能夠確定每乙個child的 right 和 bottom 或者 left,top,那麼就可以遍歷每乙個...

WPF 自定義面板

先上效果圖吧 實現自定義面板主要實現兩個方法 一 measureoverride 用於計算容器內部元素大小,這個很簡單,略過 二 arrangeoverride 用於計算容器內部元素位置和其他顯示方式,以下是實現上面布局的 protected override system.windows.size...