zoukankan      html  css  js  c++  java
  • 使用 Daynamic 动态添加属性

    所谓的Dynamic 动态类型,页面也要是动态的(强撸)

    很简单的 直接上代码:

     //案例一

    DynamicpersonCollection = new ObservableCollection();

    for (var i = 0; i < 10; i++) {

    dynamic p = new ExpandoObject();

    ((IDictionary<string, object>)p).Add("Name", "22");

    ((IDictionary<string, object>)p).Add("Age", "cc");

    DynamicpersonCollection.Add(p);

     }

    //案例二

    var DynamicpersonCollection = new ObservableCollection<dynamic>();
    for (int i = 0; i < 2; i++)
    {
    string s = "你好";
    dynamic p = new ExpandoObject();
    p.aa = "ss";
    ((IDictionary<string, object>)p).Add("GroupName" + (i + 2).ToString(), s);
    DynamicpersonCollection.Add(p);
    }

    案例三

    public class NurseScheduleStatisticsModel : DynamicObject
        {
            public string EmpName { get; set; }
            public string TotalHour { get; set; }
            public string TotalWork { get; set; }
    
            Dictionary<string, object> Properties = new Dictionary<string, object>();
    
            public override bool TrySetMember(SetMemberBinder binder, object value)
            {
                if (!Properties.Keys.Contains(binder.Name))
                {
             //在此可以做一些小动作

              //if (binder.Name == "Col")
              //  Properties.Add(binder.Name + (Properties.Count), value.ToString());
              //else
              //  Properties.Add(binder.Name, value.ToString());

                    Properties.Add(binder.Name, value.ToString());
                }
                return true;
            }
            public override bool TryGetMember(GetMemberBinder binder, out object result)
            {
                return Properties.TryGetValue(binder.Name, out result);
            }
        }
    http://www.cnblogs.com/maomiyouai/p/3594132.html
  • 相关阅读:
    crystal report 用存储过程的问题。
    新的开始—2014
    C#基础(二)——C#中的构造函数
    C#基础(一)——C#中反斜杠/n与/r的区别
    Html基础(一)
    yield关键字, default关键字, 别名关键字
    让 wpf tabcontrol 延缓初始化每个tab item content
    MVC,MVP,MVVM(补充)
    Focus scope in WPF
    Wpf ItemsControl 开启UI Virtualization 的条件
  • 原文地址:https://www.cnblogs.com/louby/p/4911158.html
Copyright © 2011-2022 走看看