原文:
WPF笔记(1.3 属性元素)——Hello,WPF!
<
Window
>
<
Button Width
=
"
100
"
Height
=
"
100
"
>
<
Image Source
=
"
tom.png
"
/>
</
Button
>
</
Window
>
也可以这样:
<
Window
>
<
Button Width
=
"
100
"
Height
=
"
100
"
>
<
TextBox Width
=
"
75
"
>
edit me
</
TextBox
>
</
Button
>
</
Window
>
就是说,将原来Button的Image属性和TextBox属性当作对象提取出来。这是因为Button起源于一个类:
ContentControl ,该类知道如何生成其装载的所有控件。
2。其实完整的写法是这样的:
<
Button Width
=
"
100
"
Height
=
"
100
"
>
<
Button.Content
>
<
Image Source
=
"
tom.png
"
/>
</
Button.Content
>
</
Button
>
但是,<Button.Content>标签内不能有两个控件,会显示语法错误,只能是一个属性元素——这时候要用Panel。
Window控件有和Button同样的用法,见下面章节。
这一节中“属性元素”的概念可以用匪夷所思形容。
1。WPF用标签元素实现对象建模,有两种:Control和Container,都用来装载内容和行为,前者如Button,后者如Window。
你可以这样写:
<
Window
>
<
Button Width
=
"
100
"
Height
=
"
100
"
>
<
Image Source
=
"
tom.png
"
/>
</
Button
>
</
Window
>
<
Window
>
<
Button Width
=
"
100
"
Height
=
"
100
"
>
<
TextBox Width
=
"
75
"
>
edit me
</
TextBox
>
</
Button
>
</
Window
>
2。其实完整的写法是这样的:
<
Button Width
=
"
100
"
Height
=
"
100
"
>
<
Button.Content
>
<
Image Source
=
"
tom.png
"
/>
</
Button.Content
>
</
Button
>
Window控件有和Button同样的用法,见下面章节。