zoukankan      html  css  js  c++  java
  • XAML系列学习

    在XAML中为属性赋值

    1、使用Attribute=value形式

    <Rectangle Width="100" Height="100" Stroke="Red" Fill="LightGreen" RadiusX="10" RadiusY="10"></Rectangle>

    2、使用属性标签的形式

    <Window.Resources>
            <local:Animal x:Key="animal" name="Dog" newAnimal="ant"></local:Animal>
    </Window.Resources>
            private void Show_Click(object sender, RoutedEventArgs e)
            {
                Animal animal =(Animal)this.FindResource("animal");
                MessageBox.Show(animal.newAnimal.name);
            }

    这时提示的是Animal这个类不支持从String进行转换,那么使用TypeConverter类将XAML标签的Attribute与对象的Property进行映射

        [TypeConverterAttribute(typeof(StringToAnimalTypeConverter))]
        public class Animal
        {
            public string name { get; set; }
            public Animal newAnimal { get; set; }
        }
    
        public class StringToAnimalTypeConverter:TypeConverter
        {
            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
            {
                if (value is string)
                {
                    Animal animal = new Animal();
                    animal.name = value as string;
                    return animal;
                }
                return base.ConvertFrom(context, culture, value);
            }
        }

    public class StringToAnimalTypeConverter:TypeConverter-------------------using System.ComponentModelTypeConverter

    提供一种将值类型转换为其他类型以及访问标准类型的值和属性的统一方法

    还需要将[TypeConverterAttribute(typeof(StringToAnimalTypeConverter))] 这个特性添加到类Animal上,表明其类型是可以转换的;[TypeConverter(typeof(StringToAnimalTypeConverter))],之所以加上Attribute是方便好看


     

    3、使用扩展标签的形式

  • 相关阅读:
    excel处理经纬度
    Bootstrap:弹出框和提示框效果以及代码展示
    c# 多张图片合成一张图片
    webapi------宿主程序
    git安装完设置成中文
    邮件帮助类
    IISHelper操作iis
    删除数组中的重复项
    SQL 、LINQ日前比较
    PostAsync与GetAsync
  • 原文地址:https://www.cnblogs.com/chenyongblog/p/3377971.html
Copyright © 2011-2022 走看看