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>