wpf-基础-xaml-标记扩展&指令元素

标记扩展

名称说明
x:Type声明数据类型,比如自定义一些按钮/组件
x:Null表示空值。如果一个属性具有默认值但不想用那个值,就要显式设置null
x:Array传递一个已知类型的ArrayList实例(例子是前端自己展示数据)
x:Static访问c#类中static成员(变量和函数)如果一个程序需要国际化支持,此时将需要显示的字符串存在一个资源类的static属性中,那么会多次使用x:Static

x:Type示例

新建一个MyWindow.xaml作为子窗口,内容如下
在这里插入图片描述
MyWindow.xaml.cs

自定义了一个button的子类,带一个Type类型的属性UserWindowType。这个类还重写了OnClick,如果属性UserWindowType是Window(及其子类),那就要展示出来。
在这里插入图片描述
MainWindow.xaml

因为MyButton和MyWindow都是命名空间pxy中的,所以要加前缀local映射。现在用x:Type说明UserWindowType就是MyWindow类型的,而MyWindow是Window的派生类,所以就要展示出来了。
在这里插入图片描述
MainWindow.xaml.cs
在这里插入图片描述
最终实现的效果是点击主窗口的Show按钮,弹出另一个窗口:
在这里插入图片描述
在这里插入图片描述

x:Null示例

    <Window.Resources>
        <Style x:Key="{x:Type Button}" TargetType="{x:Type Button}">
            <Setter Property="Width" Value="60"/>
            <Setter Property="Height" Value="36"/>
            <Setter Property="Margin" Value="5"/>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button />
        <Button />
        <Button  Style="{x:Null}"/>
    </StackPanel>

< Style >把所有Button设置成一种格式了(Margin表示四周5个像素的留白),对于最后一个,则按照默认格式来,效果:
在这里插入图片描述

x:Array示例

<Window x:Class="pxy.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:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:pxy"
        mc:Ignorable="d"
        Title="MainWindow" Height="170" Width="200">
    <Grid Background="LightBlue">
        <ListBox Margin="5">
            <ListBox.ItemsSource>
                <x:Array Type="sys:String">
                    <sys:String>AA</sys:String>
                    <sys:String>BB</sys:String>
                    <sys:String>CC</sys:String>
                </x:Array>
            </ListBox.ItemsSource>
        </ListBox>
    </Grid>
</Window>

在解析<x:Array>标签时,编译器会产生调用AddChild方法的代码把< x:Array >的子元素逐个添加到x:Array的实例Items里。效果:
在这里插入图片描述

x:Static示例

为MainWindow类添加static成员。

MainWindow.xaml.cs

namespace pxy
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static string WindowTitle = "我是窗口";
        public static string ShowText { get { return "你好!"; } }
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

MainWindow.xaml:Title使用静态成员变量,TextBox的内容使用静态成员函数的返回值。

<Window x:Class="pxy.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:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:pxy"
        mc:Ignorable="d"
        Title="{x:Static local:MainWindow.WindowTitle}" Height="170" Width="200">
    <Grid Background="LightBlue">
        <TextBox FontSize="45" Text="{x:Static local:MainWindow.ShowText}"/>
    </Grid>
</Window>

在这里插入图片描述

指令元素

x:Code 包含一些c#代码

x:XData 数据提供者标签下存放数据的标签

数据源:包含数据的对象

数据提供者:把数据源中的数据提供给数据使用者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值