Wpf 对象属性赋值---属性标签
1。给一个stop按钮赋予一个stop的图形。
如图:
代码如下:
View Code
1 <Window x:Class="wpfTest.MainWindow"
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:wpfTest"
5 Title="MainWindow" Height="350" Width="525">
6 <Window.Resources>
7 <local:Human x:Key="wtqHum" name="wtq" child="thisismyname" ></local:Human>
8
9 </Window.Resources>
10
11 <Grid>
12
13 <Button Width="100" Height="30">
14 <Button.Content>
15 <Rectangle Width="15" Height="15" Stroke="Blue" Fill="Red"></Rectangle>
16 </Button.Content>
17
18 </Button>
19 </Grid>
20 </Window>
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:wpfTest"
5 Title="MainWindow" Height="350" Width="525">
6 <Window.Resources>
7 <local:Human x:Key="wtqHum" name="wtq" child="thisismyname" ></local:Human>
8
9 </Window.Resources>
10
11 <Grid>
12
13 <Button Width="100" Height="30">
14 <Button.Content>
15 <Rectangle Width="15" Height="15" Stroke="Blue" Fill="Red"></Rectangle>
16 </Button.Content>
17
18 </Button>
19 </Grid>
20 </Window>
2、给一个矩形区域使用渐变
如图:
代码如下:
View Code
1 <Grid>
2 <Rectangle Width="300" Height="200">
3 <Rectangle.Fill>
4 <LinearGradientBrush>
5 <LinearGradientBrush.StartPoint>
6 <Point X="0" Y="0"/>
7 </LinearGradientBrush.StartPoint>
8 <LinearGradientBrush.EndPoint>
9 <Point X="1" Y="0"></Point>
10 </LinearGradientBrush.EndPoint>
11 <LinearGradientBrush.GradientStops>
12 <GradientStopCollection>
13 <GradientStop Offset="0.1" Color="Red"/>
14 <GradientStop Offset="0.4" Color="GreenYellow"/>
15 <GradientStop Offset="0.9" Color="Blue"/>
16 <GradientStop Offset="1" Color="Azure"/>
17 </GradientStopCollection>
18 </LinearGradientBrush.GradientStops>
19
20 </LinearGradientBrush>
21
22 </Rectangle.Fill>
23
24
25 </Rectangle>
26
27 </Grid>
2 <Rectangle Width="300" Height="200">
3 <Rectangle.Fill>
4 <LinearGradientBrush>
5 <LinearGradientBrush.StartPoint>
6 <Point X="0" Y="0"/>
7 </LinearGradientBrush.StartPoint>
8 <LinearGradientBrush.EndPoint>
9 <Point X="1" Y="0"></Point>
10 </LinearGradientBrush.EndPoint>
11 <LinearGradientBrush.GradientStops>
12 <GradientStopCollection>
13 <GradientStop Offset="0.1" Color="Red"/>
14 <GradientStop Offset="0.4" Color="GreenYellow"/>
15 <GradientStop Offset="0.9" Color="Blue"/>
16 <GradientStop Offset="1" Color="Azure"/>
17 </GradientStopCollection>
18 </LinearGradientBrush.GradientStops>
19
20 </LinearGradientBrush>
21
22 </Rectangle.Fill>
23
24
25 </Rectangle>
26
27 </Grid>
上面例子代码的改进。
如下:
View Code
1 <Window x:Class="wpf.Window1"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="Window1" Height="300" Width="300">
5 <Grid>
6 <Rectangle Width="300" Height="200">
7 <Rectangle.Fill>
8 <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
9 <LinearGradientBrush.GradientStops>
10 <GradientStop Offset="0.1" Color="Yellow"/>
11 <GradientStop Offset="0.5" Color="blue"/>
12 </LinearGradientBrush.GradientStops>
13 </LinearGradientBrush>
14 </Rectangle.Fill>
15
16 </Rectangle>
17 </Grid>
18 </Window>
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="Window1" Height="300" Width="300">
5 <Grid>
6 <Rectangle Width="300" Height="200">
7 <Rectangle.Fill>
8 <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
9 <LinearGradientBrush.GradientStops>
10 <GradientStop Offset="0.1" Color="Yellow"/>
11 <GradientStop Offset="0.5" Color="blue"/>
12 </LinearGradientBrush.GradientStops>
13 </LinearGradientBrush>
14 </Rectangle.Fill>
15
16 </Rectangle>
17 </Grid>
18 </Window>
原则。1,能使用attribute=value对属性赋值,尽量使用这种方式。
2,若使用默认值的属性,则可以不用写出来,
比如:
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1"> 默认的StartPoint为“”0,0“ 默认的EndPoint为”1,1“则可以不写出来。如下:
<LinearGradientBrush>
参考:属性标签