WPF x:Null

X:Null:对一个属性赋值空值。
很多时候我们不需要显式地设置null值,但如果一个属性具有默认值而我们又不需要这个默认值的时候就需要显式地设置null值了。
在WPF中,要设置按钮的样式的就就要使用Style了,Style的作用就是设置各个控件的属性,也可以为一个Style指定目标控件类型,如果指定了目标控件类型的话那么这类控件的实例就会都使用这个Style了,如果不想使用这个Style的话,就把Style属性设置为x:Null。

下面是赋空值的例子:

<Window x:Class="WpfApplication1.Window"
        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:WpfApplication1"
        mc:Ignorable="d"
        Title="Window" Height="300" Width="300">
    <Window.Resources>
        <Style x:Key="{x:Type Button}" TargetType="{x:Type Button}">
            <Setter Property="Width" Value="100"></Setter>
            <Setter Property="Height" Value="30"></Setter>
            <Setter Property="Margin" Value="5"></Setter>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button Content="按钮1"></Button>
        <Button Content="按钮2"></Button>
        <Button Style="{x:Null}" Content="按钮3"></Button>
    </StackPanel>
</Window>
上面例子将Style样式放在Window的资源里并把它的x:key和TargertType都设置成了Button类型,这样,UI上的所有Button控件都会默认的引用这个Style样式,除了最后一个Button按钮,因为最后一个按钮我们已经把它的Style样式设置成了x:Null空值了,所以的话这个按钮里面的样式是没有的。

显示效果如下图所示:

在这里插入图片描述
虽说Style设置为x:Null了,但是在控件里面还是可以添加样式的。
如下例所示:

<StackPanel>
        <Button Content="按钮1"></Button>
        <Button Content="按钮2"></Button>
        <Button Style="{x:Null}" Content="按钮3" Width="80" Height="100"></Button>
 </StackPanel>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值