-
该控件叫
Summary,
主要是一些汇总信息的显示,
有几个地方用,
之前都是
分散到各个
XAML
文件里,不统一。
本人
WPF
新手,
对
XAML
了解不多,
做这个软件基本上都是用
CM
,
界面布局用
AvalonDock
。
由于缺乏相关经验,
又没有一个能问得上的人指导,
写这个控件费了我很长时间
(啥时有空啥时
动动)。
之前主要做一些功能方面的,
< br>没有心思美化界面,
现在虽然还有很多功能没写,
但是基
本上够自
己用了,放下心思来做一些界面上的东西,要不然何苦选择
WPF
?
先看一下图:
该
CustomControl
由<
/p>
4
部分组成:
大标题,小标题,值
及
Detail
。
虽然细分这么多,但实质上还是一个列表类的控件,所以选择继承自
ItemsControl.
做
这个控件的时候,遇到了一些详细的问题不知道怎么解决,
Google/Bing
都没有找到我要了
解的,
Baidu
更不用提了,漫天的转载,
Copy.
1,
类似
ComboBox
的
DisplayMemberPath
如何弄?
既然都自定控件了,当然是想让它适用不同场景,
不能局限于某一个实体类,最好是能像
DisplayMemberPath
ValueMemberPath
这样的属性。
这里,我定义了:
ItemTitlePathProperty
及
ItemValuePathProperty
来处理。
2
,有了上面两个依赖属性,但是还不足以处理更多的场景,最好是能有不同的
Template
。
这里我定义了:
TitleTemplate / ValueTemplate
及
DetailTemplate.
第一个问题,只需定义一个简单的
DataTemplate
,然后用
TemplateBinding
即可做到。
但是要和第二个问题结合在一起,
就牵扯到模板切换
及
数据切换的问题,
因为第一问题所用的
数据被指定为某实体类的某个属性,第二个问题的数据要指
定为整个实体。
解决第一个问题,需要重写
PrepareContainerForItemOverride
方法,第二个需要重写
OnApplyTemplate
不多说了,怕误导大家,毕竟,我懂的不多,上代码,供参考:
模板文件:
1
<
ResourceDictionary
xmlns
=
2
xmlns:x
=
3
xmlns:local
=
4
>
5
6
7
<
DataTemplate
x:Key
=
8
<
TextBlock
Text
p>
=
{Binding}
Foreground
=
/>
9
Da
taTemplate
>
10
11
<
DataTemplate
x:Key
=
12
<
Border
Horizonta
lAlignment
=
Background
=
CornerRadius
=
13
<
TextBlock
Text
p>
=
{Binding}
Padding
=
Foreground
=
/>
14
Bo
rder
>
15
Da
taTemplate
>
16
17
18
<
Style
TargetType
=
{x:Type
local:Summary}
19
<
Setter
Property
=
20
<
>
21
<
ControlTemplate
TargetType
=
{x:Type
local:Summary}
22
<
Border
Backgroun
d
=
{TemplateBinding
Background}
23
Bor
derBrush
=
{TemplateBinding
BorderBrush}
24
Bor
derThickness
=
{TemplateBindin
g
BorderThickness}
25
>
26
27
<
StackPanel
Height
=
28
<
Border
Padding
=
CornerRadius
=
Background
=
29
<
TextBlock
Text
=
{Binding
RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type
local:Summary}},
Path=Title}
FontSize
=
Foreground
=
Horiz
ontalAlignment
=
/>
30
Border
>
31
<
Border
Background
=
CornerRadius
=
Padding
=
32
<
ItemsPresenter
/>
33
Bo
rder
>
34
St
ackPanel
>
35
36
Bo
rder
>
37
Co
ntrolTemplate
>
38
>
39
<
/p>
Setter
>
40
<
/p>
Style
>
41
42
<
Style
TargetType
=
{x:Type
local:SummaryItem}
43
<
Setter
Property
=
44
<
>
45
<
ControlTemplate
TargetType
=
{x:Type
local:SummaryItem}
46
<
Border
x:Name
=
47
Padding
=
48
Bor
derBrush
=
{TemplateBinding
BorderBrush}
49
Bor
derThickness
=
{TemplateBindin
g
BorderThickness}
50
51
52
<
ound
>
53
<
SolidColorBrush
/>
54
ou
nd
>
55
56
<
Sta
ckPanel
>
57
<
Doc
kPanel
>
58
<
ContentControl
x:Name
=
59
p>
Content
=
{TemplateBi
nding Title}
60
Con
tentTemplate
=
{StaticResource
SummaryTitleTemplate}
61
/>
62
63
<
ContentControl
x:Name
=
64
p>
Content
=
{TemplateBi
nding Value}
65
Con
tentTemplate
=
{StaticResource
SummaryValueTemplate}
66
Width
=
67
=
68
HorizontalAlign
ment
=
69
/>
70
71
<
/p>
DockPanel
>
72
<
ContentControl
Margin
=
x:Name
=
ContentTe
mplate
=
{TemplateBinding
DetailTemplate}
/>
73
St
ackPanel
>
74
75
<
rs
>
76
<
EventTrigger
RoutedEvent
=
77
<
p>
BeginStoryboard
>
78
<
p>
Storyboard
>
79
<
ColorAnimation
To
=
Duration
=
80
Name
=
81
Property
=
82
<
/p>
Storyboard
>
83
<
/p>
BeginStoryboard
>
84
<
/p>
EventTrigger
>
85
<
EventTrigger
RoutedEvent
=
86
<
p>
BeginStoryboard
>
87
<
p>
Storyboard
>
88
<
ColorAnimation
To
=
Duration
=
89
Name
=
90
Property
=
91
<
/p>
Storyboard
>
92
<
/p>
BeginStoryboard
>
93
<
/p>
EventTrigger
>
94
rs
>
95
96
97
<
/p>
Border
>
98
<
/p>
ControlTemplate
>
99
>
100
Se
tter
>
101
St
yle
>
102
103
ResourceDictionary
>
1
using
System;
2
using
c;
3
using
Model;
4
using
5
using
6
using
7
using
s;
8
using
ls;
9
using
10
using
nts;
11
using
12
using
13
using
g;
14
using
tion;
15
using
16
17
namespace
ls {
18
public
class
Summary : ItemsControl
{
19
20
21
public
static
DependencyProperty
TitleProperty =
er(
,
t
ypeof
(
string
),
typeof
(Summary));
22