深入浅出WPF 第一部分(5)

本文详细介绍了XAML中的几种常用标记扩展,包括x:Type、x:Null、x:Array、x:Static等,并通过示例代码展示了它们的具体用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值