WPF_03

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>

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值