WPF裡實現imageButton的步驟

2022-01-18 16:17:00 字數 3732 閱讀 8871

1、建立乙個新的類庫並寫乙個類:imagebutton,**如下:

using

system;

using

system.collections.generic;

using

system.linq;

using

system.text;

using

system.windows;

using

system.windows.controls;

using

system.windows.media;

namespace

my.controls

set }

//////

滑鼠移到按鈕上時,按鈕的背景色

/// public

brush mouseoverbackground

set }

//////

當滑鼠按下時,按鈕的前景色

/// public

brush mousedownforeground

set }

//////

當滑鼠按下時,按鈕的背景色

/// public

brush mousedownbackground

set }

//////

當按鈕不可用時,按鈕的前景色

/// public

brush disabledforeground

set }

//////

當按鈕不可用時,按鈕的背景色

/// public

brush disabledbackground

set }

#endregion

#region 依賴屬性

//////當滑鼠移到按鈕上時,按鈕的前景色(這是依賴屬性)

/// public

static

readonly dependencyproperty mouseroverforegroundproperty =dependencyproperty.register(

"mouseroverforeground

", typeof(brush), typeof(textbutton), new

propertymetadata(brushes.black));

//////

滑鼠移到按鈕上時,按鈕的背景色(這是依賴屬性)

/// public

static

readonly dependencyproperty mouseoverbackgroundproperty =dependencyproperty.register(

"mouseoverbackground

", typeof(brush), typeof(textbutton), new

propertymetadata(brushes.white));

//////

當滑鼠按下時,按鈕的前景色(這是依賴屬性)

/// public

static

readonly dependencyproperty mousedownforegroundproperty =dependencyproperty.register(

"mousedownforeground

", typeof(brush), typeof(textbutton), new

propertymetadata(brushes.black));

//////

當滑鼠按下時,按鈕的背景色(這是依賴屬性)

/// public

static

readonly dependencyproperty mousedownbackgroundproperty =dependencyproperty.register(

"mousedownbackground

", typeof(brush), typeof(textbutton), new

propertymetadata(brushes.white));

//////

當按鈕不可用時,按鈕的前景色(這是依賴屬性)

/// public

static

readonly dependencyproperty disabledforegroundproperty =dependencyproperty.register(

"disabledforeground

", typeof(brush), typeof(textbutton), new

propertymetadata(brushes.black));

//////

當按鈕不可用時,按鈕的背景色(這是依賴屬性)

/// public

static

readonly dependencyproperty disabledbackgroundproperty =dependencyproperty.register(

"disabledbackground

", typeof(brush), typeof(textbutton), new

propertymetadata(brushes.white));

#endregion

#region 建構函式

public textbutton():base

()

#endregion

}}

2、加乙個樣式檔案 style.xaml

"

"xmlns:x="

"xmlns:local="

clr-namespace:my.controls

">

3、在使用的form裡先新增引用,如下:

"

main.usercontrols.evidencepackagelist_error

"x:name="

evidencepackagelist

"xmlns="

"xmlns:x="

"xmlns:mc="

"xmlns:d="

"xmlns:dxg="

"xmlns:dxb="

"xmlns:controls="clr-namespace:my.controls;assembly=my.controls"

mc:ignorable="d"

d:designheight="

450" d:designwidth="

800" loaded="

usercontrol_loaded

">

4、使用方式如下:

"

20" width="

20" cursor="

hand

" command="

" commandparameter="

" normalbackgroundimage="

" mouseoverbackgroundimage="

"/>

5、效果如下:

WPF裡ItemsControl的分組實現

原文 wpf裡itemscontrol的分組實現 我們在用到itemscontrol時,有時會用到分組,如listbox,listview,datagrid。wpf的itemscontrol可以實現分組,是依託於groupstyle,以listbox為例,他的分組效果圖為 以下為前台 1 lbmai...

WPF裡ItemsControl的分組實現

我們在用到itemscontrol時,有時會用到分組,如listbox,listview,datagrid。wpf的itemscontrol可以實現分組,是依託於groupstyle,以listbox為例,他的分組效果圖為 以下為前台 1 2 3 4 5 7 9 11 12 13 14 15 16 ...

WPF學習筆記 在XAML裡繫結

binding除了預設建構函式外,還有乙個可以傳入path的建構函式,下面兩種方式實現的功能是一樣的。text backgroud aliceblue fontsize 16 text backgroud aliceblue fontsize 16 這裡使用elementname來設定源物件,而沒有...