4.3 x名称空间中的标记扩展
4.3.1 x:Type
当我们在XAML中想表达某个数据类型时就需要使用x:Type标记扩展。
//MyButton.cs
using System;
using System.Windows;
using System.Windows.Controls;
namespace FirstWpfApplication.Controls
{
class MyButton:Button
{
public Type UserWindowType { get; set; }
protected override void OnClick()
{
base.OnClick();
Window window = Activator.CreateInstance(this.UserWindowType) as Window;
if (window != null)
window.ShowDialog();
}
}
}
<Window x:Class="FirstWpfApplication.MainWindow"
x:ClassModifier="public"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:buttons="clr-namespace:FirstWpfApplication.Controls"
xmlns:local="clr-namespace:FirstWpfApplication"
Title="Anders" Height="200" Width="200" >
<StackPanel>
<buttons:MyButton UserWindowType="{x:Type TypeName=local:Window1 }" Margin="5" Content="OK"/>
</StackPanel>
</Window>
4.3.2 x:Null
在C#语言中,使用null关键字来表示空值,在XAML里用来表示空值的是x:Null。
<Window x:Class="FirstWpfApplication.MainWindow"
x:ClassModifier="public"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Anders" Height="200" Width="200" >
<StackPanel>
<Button x:Name="button1" Content="button1"/>
<Button x:Name="button2" Content="button2"/>
<Button x:Name="button3" Content="button3" Style="{x:Null}"/>
<!--x:Null-->
</StackPanel>
<Window.Resources>
<Style x:Key="{x:Type TypeName=Button}" TargetType="{x:Type TypeName=Button}">
<!--x:Type-->
<Setter Property="Width" Value="60"/>
<Setter Property="Height" Value="36"/>
<Setter Property="Margin" Value="5" />
</Style>
</Window.Resources>
</Window>
4.3.4 x:Array
x:Array的作用就是通过它的Items属性向使用者暴露一个类型已知的ArrayList实例,ArrayList内成员的类型由x:Array的Type指明。
<Window x:Class="FirstWpfApplication.MainWindow"
x:ClassModifier="public"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Anders" Height="500" Width="200" >
<StackPanel>
<ListBox Margin="5" Height="100">
<ListBox.ItemsSource>
<x:Array Type="{x:Type TypeName=sys:String}">
<sys:String>Tim</sys:String>
<sys:String>Tom</sys:String>
<sys:String>Anders</sys:String>
</x:Array>
</ListBox.ItemsSource>
</ListBox>
</StackPanel>
</Window>
4.3.5 x:Static
x:Static是一个很常用的标记扩展,它的功能是在XAML文档中使用数据类型的static成员。因为XAML不能编写逻辑代码,所以使用x:Static访问Static成员一定是数据类型的属性或字段。
namespace FirstWpfApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static string WindowTitle = "大话西游";
public static string WindowContent = "月光宝盒";
}
}
<Window x:Class="FirstWpfApplication.MainWindow"
x:ClassModifier="public"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FirstWpfApplication"
Title="{x:Static Member=local:MainWindow.WindowTitle}" Height="500" Width="200" >
<StackPanel>
<TextBlock FontSize="32" Text="{x:Static Member=local:MainWindow.WindowContent}" Height="200" Margin="5"/>
</StackPanel>
</Window>
4.4 XAML指令元素
x:XData标签是一个专用标签。WPF中把包含数据的对象称之为数据源,用于把数据源中的数据提供给数据使用者的对象称之为数据提供者(Data Provider)。WPF类库中包含多种数据提供者,其中一个类叫XmlDataProvider,专门用于提供XML化的数据。
<Window.Resources>
<XmlDataProvider x:Key="InventoryData" XPath="Inventory/Books">
<x:XData>
<Fruits>
<Fruit Name="Peach"/>
</Fruits>
</x:XData>
</XmlDataProvider>
</Window.Resources>