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

  • 相关阅读:
    SQL 2008 数据库只读 修改
    java List 简单使用
    鼠标右键菜单 删除
    SQL distinct
    日系插画学习笔记(五):日系角色脸部画法-1头部
    日系插画学习笔记(四):基础人体结构
    日系插画学习笔记(三):光影与结构
    日系插画学习笔记(二):结构与透视
    日系插画学习笔记(一):SAI软件基础
    spring mvc 静态资源版本控制
  • 原文地址:https://www.cnblogs.com/chenyongblog/p/3377971.html
Copyright © 2011-2022 走看看