效果如下:
首先是自定義按鈕類:
public
class textimagebutton:button
private
void textimagebutton_isselectedchanged(
object sender, dependencypropertychangedeventargs e)
else
}
public
delegate
void isselectedchangedeventhandler(
object sender, dependencypropertychangedeventargs e);
public
event isselectedchangedeventhandler isselectedchanged;
private
bool isselected =
false;
public
bool isselected
set
}
}
public
newstatic dependencyproperty backgroundproperty = dependencyproperty.registerattached
(
"background
",
typeof(imagesource),
typeof(button)
);
public
new imagesource background
set
}
public
static dependencyproperty textcolorproperty = dependencyproperty.registerattached
(
"textcolor
",
typeof(solidcolorbrush),
typeof(button)
);
public solidcolorbrush textcolor
set
}
public
static dependencyproperty textproperty = dependencyproperty.registerattached
(
"text
",
typeof(
string),
typeof(button)
);
public
string text
set
}
public imagesource selectedbackground
public imagesource unselectedbackground
public solidcolorbrush selectedtextcolor
public solidcolorbrush unselectedtextcolor
}
其次是建立按鈕模板:
<
controltemplate
targettype
="button"
x:key
="selectbutton"
>
<
grid
cursor
="hand"
>
<
image
source="}
">
image
>
<
textblock
fontfamily
="微軟雅黑"
fontsize
="24"
width
="220"
horizontalalignment
="center"
verticalalignment
="center"
textalignment
="center"
text="}
"foreground="}
"="wrap"
>
textblock
>
grid
>
controltemplate
>
最後就可以在後台中直接動態建立:
textimagebutton tbi =
new textimagebutton()
;
變換按鈕的狀態通過 tbi.isselected = true 或 tbi.isselected = false 即可
自定義WPF的Transform
我們都知道wpf的transform有很多種,比如scaletransform,rotatetransform什麼的。而且從純粹的數學角度來講,這些transform的組合,可以完全任何二維線性圖形變換。完全沒有必要自己再去寫乙個。但是純數學,總是純數學的。比如我想做這樣乙個transform,它總...
自定義圓形背景的TextView
繼承textview類,首先是構造方法,兩參 構造是解析布局檔案時呼叫的,所以相關的操作在這個方法中定義.在ondraw 方法裡,使用canvas.drawcircle float cx,float cy,float radius,paint paint 方法繪製乙個圓.圓的顏色由paint引數決定...
WPF 自定義內容的彈窗
目標是重複使用固定的彈窗樣式,並提供確認和取消按鈕,而彈窗的具體內容則由傳參決定。為了達到目的,使用contentcontrol設定內容,並通過新增showdialog方法為確認按鈕新增業務邏輯。效果如圖 綠色框的部分為自定義內容 關鍵 設定contentcontrol public customd...