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、使用扩展标签的形式

  • 相关阅读:
    linux 安装python3
    phoenix 索引实践
    spark shuffle参数调优
    hbase 面试问题汇总
    sqlserver 自动初始化从节点数据
    hive sql 窗口函数
    数据仓库建模
    Spark DataFrame简介(二)
    list删除时java.util.ConcurrentModificationException
    mybatis中判断等于字符串的条件怎么写
  • 原文地址:https://www.cnblogs.com/chenyongblog/p/3377971.html
Copyright © 2011-2022 走看看