对于Control而言,在XAML中设置的属性有两种语法,一种是XML元素(element)的Attribute,另一种为元素的Proptery
例如
1.使用XML元素Attribute设置按钮的Content属性和Background属性
1
<
Button
Content
="Hello World"
Background
="Blue"
/>
2
2
2.使用XML元素Proptery设置上述按钮属性
1
<
Button
>
2 < Button.Content >
3 Hello World
4 </ Button.Content >
5
6 < Button.Background >
7 Blue
8 </ Button.Background >
9 </ Button >
2 < Button.Content >
3 Hello World
4 </ Button.Content >
5
6 < Button.Background >
7 Blue
8 </ Button.Background >
9 </ Button >
3.Content是C# ContentPropertyAttribute定义过的所以在XAML中可以直接作为XML元素的值出现
<
Button
Background
="Blue"
>
Hello World
</ Button >
Hello World
</ Button >
4.使用XML元素Proptery形式,对于Control的Style进行设置
1
<
Button
Content
="Hello World"
>
2 < Button.Resources >
3 < Style x:Key ="ButtonStyler" TargetType =" {x:Type Button} " >
4 < Setter Property ="Background" >
5 < Setter.Value >
6 < RadialGradientBrush >
7 < GradientBrush.GradientStops >
8 < GradientStopCollection >
9 < GradientStop Color ="black"
10 Offset ="0" />
11 < GradientStop Color ="black"
12 Offset ="1" />
13 </ GradientStopCollection >
14 </ GradientBrush.GradientStops >
15 </ RadialGradientBrush >
16 </ Setter.Value >
17 </ Setter >
18 </ Button.Resources >
19
20 < Button.Style >
21 < StaticResource >
22 < StaticResource.ResourceKey >
23 ButtonStyler
24 </ StaticResource.ResourceKey >
25 </ StaticResource >
26 </ Button.Style >
27 </ Button >
2 < Button.Resources >
3 < Style x:Key ="ButtonStyler" TargetType =" {x:Type Button} " >
4 < Setter Property ="Background" >
5 < Setter.Value >
6 < RadialGradientBrush >
7 < GradientBrush.GradientStops >
8 < GradientStopCollection >
9 < GradientStop Color ="black"
10 Offset ="0" />
11 < GradientStop Color ="black"
12 Offset ="1" />
13 </ GradientStopCollection >
14 </ GradientBrush.GradientStops >
15 </ RadialGradientBrush >
16 </ Setter.Value >
17 </ Setter >
18 </ Button.Resources >
19
20 < Button.Style >
21 < StaticResource >
22 < StaticResource.ResourceKey >
23 ButtonStyler
24 </ StaticResource.ResourceKey >
25 </ StaticResource >
26 </ Button.Style >
27 </ Button >