X命名空间-标记扩展

标记扩展一般含有 {} 符号

 

x:Type 传入操作数据类型

x:Null 空值


通过例子看下具体:

(前台XAML代码)

<Window x:Class="WpfApplication1.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"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="{x:Type local:UCButton}" TargetType="Button">
            <Setter Property="Width" Value="80"/>
            <Setter Property="Height" Value="30" />
            <Setter Property="FontFamily" Value="Comic Sans MS" />
            <Setter Property="Foreground" Value="Blue"/>
        </Style>        
    </Window.Resources>
    <Grid>
        <local:UCButton myType="{x:Type Button}" Style="{x:Null}"  Margin="83,140,340,141" Content="Button 类型"    />
        <local:UCButton myType="{x:Type Window}"  Margin="305,140,118,141"  Content="Window 类型"   />
        <local:UCButton myType="{x:Type TextBlock}"  Margin="195,140,228,141" Content="TextBlock 类型"  />
    </Grid>
</Window>

 

(后台C#代码 ,UCButton.cs)

 

 public class UCButton:Button
    {
        public Type myType{get;set;}     //传入类型

        /// <summary>
        /// 重写点击事件
        /// </summary>
        protected override void OnClick()
        {
            base.OnClick();

            //所有控件基类,反射生成对象
            FrameworkElement element = Activator.CreateInstance(this.myType) as FrameworkElement;

            if (element != null)
            {
                MessageBox.Show(element.GetType().Name);    //显示对应类型
            }
        }
    }

 


上面用到地方有两处,

1.页面资源样式共享,目标对象UCButton,然后 Style="{x:Null}",不用于当前按钮。

2.传入类型不同,点击显示不同。

效果图:

 

x:Array 数组源对象

 <ListBox BorderBrush="AliceBlue" BorderThickness="2"  Width="80" >
            <ListBox.ItemsSource>            
                <x:Array Type="sys:String">
                    <sys:String>X</sys:String>
                    <sys:String>A</sys:String>
                    <sys:String>M</sys:String>
                    <sys:String>L</sys:String>
                </x:Array>
            </ListBox.ItemsSource>
</ListBox>

 

 

x:Static

引用静态变量

public static string WindowText = "静态变量";

  <Button  Width="80" Height="30" Content="{x:Static local:MainWindow.WindowText}"/>

 

转载于:https://www.cnblogs.com/linsage/archive/2012/10/02/2710446.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值