zoukankan      html  css  js  c++  java
  • 3 自定义控件之复杂属性连字符形式属性

    1 连字符形式的复杂属性

       image

    连字符形式的复杂属性:通过"复杂属性名称-子属性" 字符串格式提供追加到主控件的属性。

    2 相关特性                       
        2.1 PersistenceMod                                           
        持久化模式                                       
            Attribute
            InnerProperty :                                       
            InnerDefaultProperty :                                       
            EncodedInnerDefaultProperty:                   
        2.2 DesignerSerializationVisibility                                           
            Hidden:代码生成器不生成对象的代码。                                       
            Visible:代码生成器生成对象的代码。                                       
            Content:代码生成器产生对象内容的代码,而不是对象本身的代码。                                                       
        2.3 TypeConverter                                            
            类型转化器                                       

        2.4 [NotifyParentProperty(true)]                                            
            子属性改变是否通父属性。                                       

    3 示例:

    3.1 类代码

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.ComponentModel;

    namespace AspnetEssential.CustomerControl

    {

    /// <summary>

    /// Person

    /// </summary>

    /// <remarks>

    /// 复杂属性

    /// TypeConverter指定属性转换器

    /// </remarks>

    [TypeConverter(typeof(ExpandableObjectConverter) )]

    public class Person

    {

    private string _name;

    private int _age;

    /// <summary>

    /// 姓名

    /// </summary>

    /// <remarks>

    /// NotifyParentProperty:当子属性更改时通知父属性

    /// </remarks>

    [NotifyParentProperty(true)]

    public string Name

    {

    get

    {

    return _name;

    }

    set

    {

    _name = value;

    }

    }

    /// <summary>

    /// 年龄

    /// </summary>

    [NotifyParentProperty(true)]

    public int Age

    {

    get

    {

    return _age;

    }

    set

    {

    _age = value;

    }

    }

    }

    }

    3.2 控件代码

    using System;

    using System.Collections.Generic;

    using System.Collections.Specialized;

    using System.Text;

    using System.Web;

    using System.Web.UI;

    using System.ComponentModel;

    namespace AspnetEssential.CustomerControl.ControlProperty

    {

    /// <summary>

    /// PersonPropertyControl

    /// </summary>

    /// <remarks>

    /// 连字符形式的复杂标记

    /// </remarks>

    public class PersonPropertyControl : Control

    {

    private Person _person;

    public PersonPropertyControl()

    {

    _person = new Person();

    _person.Name = "姓名";

    _person.Age = 20;

    }

    [Category("复杂属性")]

    [Description("复杂属性")]

    public Person Person

    {

    get

    {

    return _person;

    }

    }

    /// <summary>

    /// PersonPro

    /// </summary>

    /// <remarks>

    /// 指定PersistenceMode属性

    /// 指定DesignerSerializationVisibility属性

    /// </remarks>

    [Category("复杂属性")]

    [Description("复杂属性")]

    [PersistenceMode(PersistenceMode.Attribute)]

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

    public Person PersonPro

    {

    get

    {

    return _person;

    }

    }

    /// <summary>

    /// Render

    /// </summary>

    /// <param name="writer"></param>

    protected override void Render(HtmlTextWriter writer)

    {

    writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");

    writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, "blue");

    writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "1px");

    writer.RenderBeginTag(HtmlTextWriterTag.Div);

    string text = string.Format("Name:{0} Age:{1}", _person.Name, _person.Age);

    writer.Write(text);

    writer.RenderEndTag();

    }

    }

    }

    3.3 效果

    属性编辑器中的效果:

    image

    当编辑PersonPro的Age与Name后,切换到代码视图,会看到持久化的字符串PersonPro-Age,PersonPro_Name。虽然Person也可以指定子属性
    但是它没有指定[PersistenceMode(PersistenceMode.Attribute)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    在属性编辑后在值只在内存中而没有持久化到页面上。

    image

  • 相关阅读:
    Android_项目文件结构目录分析
    WPF_MVVM 开发的几种模式讨论
    Blend_技巧篇_淡入淡出
    Blend_技巧篇_导入PSD文件制作ToggleButton (Z)
    Blend_界面快速入门(Z)
    Blend_软件系列简介(Z)
    Blend_ControlTemplate(Z)
    803. 区间合并
    P4017 最大食物链计数
    P1113 杂务
  • 原文地址:https://www.cnblogs.com/hbb0b0/p/1880562.html
Copyright © 2011-2022 走看看