WPF给对象属性赋值
上一篇文档写了给对象属性赋值的第一种方法(方法一)请点击这里查看上篇文章
今天写一写第二种赋值方法
第二种:属性标签赋值
直接上代码:
<Button Grid.Row="0" Grid.Column="1" Width="200" Height="50" Content="按钮A" Click="Button_Click" FontSize="25"/>
<Button Grid.Row="0" Grid.Column="0" Width="200" Height="50" FontSize="25">
<Button.Content>
按钮B
</Button.Content>
</Button>
我们可以看到第一个button中的内容属性赋值方式:Content=“按钮A”,这是我们上次学的赋值方式。
再看看第二个button,它的内容赋值方式是在中间写子节点
<Button.Content>
按钮B
</Button.Content>
这就是属性标签的赋值方式。头尾标签中的部分写的是内容,对应Content。
使用这种方法我们就可以给对象赋更加复杂的值,举个例子。
假设我们要给Button按钮中添加一个TextBlock,通过第一种方法肯定是不行的。
就要采用第二种方法。
<Button Grid.Row="0" Grid.Column="1" Width="200" Height="50" Content="按钮A" Click="Button_Click" FontSize="25"/>
<Button Grid.Row="0" Grid.Column="0" Width="200" Height="100" FontSize="25">
<Button.Content>
<TextBlock Width="100" Height="60" Background="red">
哈哈
</TextBlock>
</Button.Content>
</Button>
下面再做一个渐变色的矩形
代码:
<Grid>
<Rectangle Width="400" Height="300" Stroke="Blue">
<Rectangle.Fill>
<LinearGradientBrush>
<LinearGradientBrush.StartPoint>
<Point X="0" Y="0"/>
</LinearGradientBrush.StartPoint>
<LinearGradientBrush.EndPoint>
<Point X="1" Y="1"/>
</LinearGradientBrush.EndPoint>
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Offset="0.33" Color="PaleVioletRed"/>
<GradientStop Offset="0.66" Color="OrangeRed"/>
<GradientStop Offset="1" Color="DarkRed"/>
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
效果就是这样。但是我们发现这代码量比较大,写起来很不方便。所以在以后写XAML代码时,能用Attribute:value这种方式的尽量就不用属性标签赋值,默认值可以不用写。我们将上面的代码简化一下。
<Grid>
<Rectangle Width="400" Height="300" Stroke="Blue">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.33" Color="PaleVioletRed"/>
<GradientStop Offset="0.66" Color="OrangeRed"/>
<GradientStop Offset="1" Color="DarkRed"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
第三种:扩展标签
例子:
<Window x:Class="WpfDay01.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfDay01"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="800">
<Window.Resources>
<sys:String x:Key="test">测试</sys:String>
</Window.Resources>
<Grid>
<TextBlock Width="300" Height="100" FontSize="50" Background="Pink" Text="{StaticResource ResourceKey=test}"/>
</Grid>
</Window>
运行结果
最后一个例子:
<Window x:Class="WpfDay01.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfDay01"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Width="100" x:Name="tb" Text="{Binding ElementName=sld,Path=Value}"/>
<Slider x:Name="sld" Grid.Row="2" Value="50" Minimum="0" Maximum="100"/>
</Grid>
</Window>