有了上篇文章的基础,这篇文章实现CustomControl就简单得多了,绝大多数代码也可以直接Copy过来使用。具体的创建CustomControl的步骤我就不累述了。
为了方便大家学习,请 单击此处 下载该程序的代码。
这次,Themes\Pager.xaml的代码如下:
其中省略号的部分用上篇文章中的Grid之间的代码代替。唯一需要修改的是:将上篇文章中,各个Label控件的MouseLeftButtonUp方法删除。
由于我曾经解释过,我们不建议在Themes目录下创建一个Pager.xaml.cs文件去处理各个Label控件的事件,所以我们需要在主程序中找到各个Label控件,并注册事件。
我们可以重写OnApplyTemplate事件,在这里去找到各个Label控件,并注册事件。
其它的代码就跟上一篇文章中的一样了。
为了方便大家学习,请 单击此处 下载该程序的代码。
这次,Themes\Pager.xaml的代码如下:
1
<
ResourceDictionary
2
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
xmlns
:
x
="http://schemas.microsoft.com/winfx/2006/xaml"
4
xmlns
:
local
="clr-namespace:Eallies.OA.UI.Controls.Common">
5
6
<
Style
TargetType
="{
x
:
Type
local
:
Pager
}">
7
<
Setter
Property
="Template">
8
<
Setter.Value
>
9
<
ControlTemplate
>
10
<
Grid
>
11
……
12
</
Grid
>
13
</
ControlTemplate
>
14
</
Setter.Value
>
15
</
Setter
>
16
</
Style
>
17
18
</
ResourceDictionary
>
由于我曾经解释过,我们不建议在Themes目录下创建一个Pager.xaml.cs文件去处理各个Label控件的事件,所以我们需要在主程序中找到各个Label控件,并注册事件。
我们可以重写OnApplyTemplate事件,在这里去找到各个Label控件,并注册事件。
1
public
override
void OnApplyTemplate()
2 {
3
try
4 {
5
base.OnApplyTemplate();
6
7
this.AttachToVisualTree();
8 }
9
catch
10 {
11
throw;
12 }
13 }
14
15
private
void AttachToVisualTree()
16 {
17
try
18 {
19
this.DetachFromVisualTree();
20
21
this.lblFirst =
this.GetTemplateChild(
"lblFirst")
as
Label;
22
this.lblPrevious =
this.GetTemplateChild(
"lblPrevious")
as
Label;
23
this.lblPage1 =
this.GetTemplateChild(
"lblPage1")
as
Label;
24
this.lblPage2 =
this.GetTemplateChild(
"lblPage2")
as
Label;
25
this.lblPage3 =
this.GetTemplateChild(
"lblPage3")
as
Label;
26
this.lblPage4 =
this.GetTemplateChild(
"lblPage4")
as
Label;
27
this.lblPage5 =
this.GetTemplateChild(
"lblPage5")
as
Label;
28
this.lblNext =
this.GetTemplateChild(
"lblNext")
as
Label;
29
this.lblLast =
this.GetTemplateChild(
"lblLast")
as
Label;
30
this.lblPages =
this.GetTemplateChild(
"lblPages")
as
Label;
31
32
if (
this.lblFirst !=
null)
this.lblFirst.MouseLeftButtonUp +=
new
MouseButtonEventHandler(lblFirst_MouseLeftButtonUp);
33
if (
this.lblPrevious !=
null)
this.lblPrevious.MouseLeftButtonUp +=
new
MouseButtonEventHandler(lblPrevious_MouseLeftButtonUp);
34
if (
this.lblPage1 !=
null)
this.lblPage1.MouseLeftButtonUp +=
new
MouseButtonEventHandler(lblPage1_MouseLeftButtonUp);
35
if (
this.lblPage2 !=
null)
this.lblPage2.MouseLeftButtonUp +=
new
MouseButtonEventHandler(lblPage2_MouseLeftButtonUp);
36
if (
this.lblPage4 !=
null)
this.lblPage4.MouseLeftButtonUp +=
new
MouseButtonEventHandler(lblPage4_MouseLeftButtonUp);
37
if (
this.lblPage5 !=
null)
this.lblPage5.MouseLeftButtonUp +=
new
MouseButtonEventHandler(lblPage5_MouseLeftButtonUp);
38
if (
this.lblNext !=
null)
this.lblNext.MouseLeftButtonUp +=
new
MouseButtonEventHandler(lblNext_MouseLeftButtonUp);
39
if (
this.lblLast !=
null)
this.lblLast.MouseLeftButtonUp +=
new
MouseButtonEventHandler(lblLast_MouseLeftButtonUp);
40 }
41
catch
42 {
43
throw;
44 }
45 }
46
47
private
void DetachFromVisualTree()
48 {
49
try
50 {
51
if (
this.lblFirst !=
null)
this.lblFirst.MouseLeftButtonUp -=
new
MouseButtonEventHandler(lblFirst_MouseLeftButtonUp);
52
if (
this.lblPrevious !=
null)
this.lblPrevious.MouseLeftButtonUp -=
new
MouseButtonEventHandler(lblPrevious_MouseLeftButtonUp);
53
if (
this.lblPage1 !=
null)
this.lblPage1.MouseLeftButtonUp -=
new
MouseButtonEventHandler(lblPage1_MouseLeftButtonUp);
54
if (
this.lblPage2 !=
null)
this.lblPage2.MouseLeftButtonUp -=
new
MouseButtonEventHandler(lblPage2_MouseLeftButtonUp);
55
if (
this.lblPage4 !=
null)
this.lblPage4.MouseLeftButtonUp -=
new
MouseButtonEventHandler(lblPage4_MouseLeftButtonUp);
56
if (
this.lblPage5 !=
null)
this.lblPage5.MouseLeftButtonUp -=
new
MouseButtonEventHandler(lblPage5_MouseLeftButtonUp);
57
if (
this.lblNext !=
null)
this.lblNext.MouseLeftButtonUp -=
new
MouseButtonEventHandler(lblNext_MouseLeftButtonUp);
58
if (
this.lblLast !=
null)
this.lblLast.MouseLeftButtonUp -=
new
MouseButtonEventHandler(lblLast_MouseLeftButtonUp);
59 }
60
catch
61 {
62
throw;
63 }
64 }
本文转自 Eallies 51CTO博客,原文链接:http://blog.51cto.com/eallies/79016,如需转载请自行联系原作者