UserControl实现起来比较简单。之所以说简单,是因为它没有多少思维的转换,大体做过.NET 2.0版的控件的人,把代码Copy过来,按照WPF的要求做一些改动即可。如果您要求快速开发,UserControl是一个不错的选择。
UserControl的实现步骤如下:
1、创建一个WPF User Control Library类型的项目。
2、将自动创建的UserControl1.xaml删除,重新添加一个名为Pager.xaml的User Control (WPF)项目。
3、将Pager.xaml的代码更改如下:
其中,Grid.RowDefinitions和Grid.ColumnDefinitions这部分用于定义行和列,也就类似与HTML中的表格。之后,设定控件的Grid.Row和Grid.Column属性,即可定位这个控件。而具体的控件的绘制,则非常类似与在ASPX页面中绘制控件。
值得注意的是:UserControl可直接在控件上捆绑事件,并在后台的xxx.xaml.cs中编写代码。而CustomControl则不建议这么处理,虽然ResourceDictionary文件也可以加上x:Class,之后即可在后台的xxx.xaml.cs中编写代码,但由于CustomControl的主体类和后台的xxx.xaml.cs类这两个实例之间的通讯存在问题,所以我们不建议这样做。想进一步了解的朋友,请参阅综合应用WPF/WCF/WF/LINQ之二十三:采用用DataTemplate的方式实现CheckListBox的CustomControl。
1
<
UserControl
x
:
Class
="Eallies.OA.UI.Controls.Common.Pager"
2
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
xmlns
:
x
="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded
="UserControl_Loaded">
4
<
Grid
>
5
<
Grid.RowDefinitions
>
6
<
RowDefinition
/>
7
</
Grid.RowDefinitions
>
8
<
Grid.ColumnDefinitions
>
9
<
ColumnDefinition
Width
="Auto" />
10
<
ColumnDefinition
Width
="Auto" />
11
<
ColumnDefinition
Width
="Auto" />
12
<
ColumnDefinition
Width
="Auto" />
13
<
ColumnDefinition
Width
="Auto" />
14
<
ColumnDefinition
Width
="Auto" />
15
<
ColumnDefinition
Width
="Auto" />
16
<
ColumnDefinition
Width
="Auto" />
17
<
ColumnDefinition
Width
="Auto" />
18
<
ColumnDefinition
Width
="22" />
19
<
ColumnDefinition
Width
="Auto" />
20
</
Grid.ColumnDefinitions
>
21
<
Label
Name
="lblFirst"
Content
="<<"
Grid.Column
="0"
MouseLeftButtonUp
="lblFirst_MouseLeftButtonUp"
VerticalAlignment
="Center"
HorizontalAlignment
="Center"
Foreground
="Blue"
Cursor
="Hand" />
22
<
Label
Name
="lblPrevious"
Content
="<"
Grid.Column
="1"
MouseLeftButtonUp
="lblPrevious_MouseLeftButtonUp"
VerticalAlignment
="Center"
HorizontalAlignment
="Center"
Foreground
="Blue"
Cursor
="Hand" />
23
<
Label
Name
="lblPage1"
Content
="1"
Grid.Column
="2"
MouseLeftButtonUp
="lblPage1_MouseLeftButtonUp"
VerticalAlignment
="Center"
HorizontalAlignment
="Center"
Foreground
="Blue"
Cursor
="Hand" />
24
<
Label
Name
="lblPage2"
Content
="2"
Grid.Column
="3"
MouseLeftButtonUp
="lblPage2_MouseLeftButtonUp"
VerticalAlignment
="Center"
HorizontalAlignment
="Center"
Foreground
="Blue"
Cursor
="Hand" />
25
<
Label
Name
="lblPage3"
Content
="3"
Grid.Column
="4"
VerticalAlignment
="Center"
HorizontalAlignment
="Center"
FontWeight
="Bold" />
26
<
Label
Name
="lblPage4"
Content
="4"
Grid.Column
="5"
MouseLeftButtonUp
="lblPage4_MouseLeftButtonUp"
VerticalAlignment
="Center"
HorizontalAlignment
="Center"
Foreground
="Blue"
Cursor
="Hand" />
27
<
Label
Name
="lblPage5"
Content
="5"
Grid.Column
="6"
MouseLeftButtonUp
="lblPage5_MouseLeftButtonUp"
VerticalAlignment
="Center"
HorizontalAlignment
="Center"
Foreground
="Blue"
Cursor
="Hand" />
28
<
Label
Name
="lblNext"
Content
=">"
Grid.Column
="7"
MouseLeftButtonUp
="lblNext_MouseLeftButtonUp"
VerticalAlignment
="Center"
HorizontalAlignment
="Center"
Foreground
="Blue"
Cursor
="Hand" />
29
<
Label
Name
="lblLast"
Content
=">>"
Grid.Column
="8"
MouseLeftButtonUp
="lblLast_MouseLeftButtonUp"
VerticalAlignment
="Center"
HorizontalAlignment
="Center"
Foreground
="Blue"
Cursor
="Hand" />
30
<
Label
Name
="lblPages"
Content
="1/10"
Grid.Column
="10"
VerticalAlignment
="Center"
HorizontalAlignment
="Center" />
31
</
Grid
>
32
</
UserControl
>
其设计图的效果如下:
4、更改Pager.xaml.cs的代码。具体代码请参考Eallies.OA系统的源代码。
这里仅仅列举隐藏和显示控件的方法。在符合一定条件的情况下,第一页、上一页、前两页、前一页、后一页、后两页、下一页、最后一页,这些控件可能显示也可能不显示。在不显示时,将容纳该控件的格的宽度设为0即可;在显示时,则设为Auto。
1
private
void SetVisible()
2 {
3
try
4 {
5 ((
Grid)
this.lblFirst.Parent).ColumnDefinitions[0].Width =
GridLength.Auto;
6 ((
Grid)
this.lblPrevious.Parent).ColumnDefinitions[1].Width =
GridLength.Auto;
7 ((
Grid)
this.lblPage1.Parent).ColumnDefinitions[2].Width =
GridLength.Auto;
8 ((
Grid)
this.lblPage2.Parent).ColumnDefinitions[3].Width =
GridLength.Auto;
9 ((
Grid)
this.lblPage4.Parent).ColumnDefinitions[5].Width =
GridLength.Auto;
10 ((
Grid)
this.lblPage5.Parent).ColumnDefinitions[6].Width =
GridLength.Auto;
11 ((
Grid)
this.lblNext.Parent).ColumnDefinitions[7].Width =
GridLength.Auto;
12 ((
Grid)
this.lblLast.Parent).ColumnDefinitions[8].Width =
GridLength.Auto;
13
14
if (
this._PageIndex <= 1) ((
Grid)
this.lblFirst.Parent).ColumnDefinitions[0].Width = (
GridLength)(
new
GridLengthConverter()).ConvertFromString(
"0");
15
if (
this._PageIndex <= 1) ((
Grid)
this.lblPrevious.Parent).ColumnDefinitions[1].Width = (
GridLength)(
new
GridLengthConverter()).ConvertFromString(
"0");
16
if (
this._PageIndex - 2 <= 0) ((
Grid)
this.lblPage1.Parent).ColumnDefinitions[2].Width = (
GridLength)(
new
GridLengthConverter()).ConvertFromString(
"0");
17
if (
this._PageIndex - 1 <= 0) ((
Grid)
this.lblPage2.Parent).ColumnDefinitions[3].Width = (
GridLength)(
new
GridLengthConverter()).ConvertFromString(
"0");
18
if (
this._PageIndex + 1 >=
this._PageCount + 1) ((
Grid)
this.lblPage4.Parent).ColumnDefinitions[5].Width = (
GridLength)(
new
GridLengthConverter()).ConvertFromString(
"0");
19
if (
this._PageIndex + 2 >=
this._PageCount + 1) ((
Grid)
this.lblPage5.Parent).ColumnDefinitions[6].Width = (
GridLength)(
new
GridLengthConverter()).ConvertFromString(
"0");
20
if (
this._PageIndex + 1 >=
this._PageCount + 1) ((
Grid)
this.lblNext.Parent).ColumnDefinitions[7].Width = (
GridLength)(
new
GridLengthConverter()).ConvertFromString(
"0");
21
if (
this._PageIndex >=
this._PageCount) ((
Grid)
this.lblLast.Parent).ColumnDefinitions[8].Width = (
GridLength)(
new
GridLengthConverter()).ConvertFromString(
"0");
22 }
23
catch
24 {
25
throw;
26 }
27 }