close

IDE:VS2017


新增一個類別

public class DisplayNameExtension : MarkupExtension
    {
        public Type Type { get; set; }

        public string PropertyName { get; set; }

        public DisplayNameExtension()
        {
        }

        public DisplayNameExtension(string propertyName)
        {
            PropertyName = propertyName;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            string name = "";
            var prop = Type.GetProperty(PropertyName);
            var attributesDisplay = prop.GetCustomAttributes(typeof(DisplayAttribute), false);
            if (attributesDisplay != null && attributesDisplay.Any())
                name = (attributesDisplay[0] as DisplayAttribute).Name;
            return name;
        }
    }


UserControl 要載入剛剛的靜態類別,以及你的ClassVM

   private string _Name;

        [Required, StringLength(50)]
        [Display(Name = "帳號")]
        public string Name{
            get { return _Name; }
            set {
                _Name= value;
                this.NotifyPropertyChanged(o => o.Name);
            }
        }

 

 <TextBlock  Text="{e:DisplayName Name,Type=vm:User}" />

這樣子就會呈現出你的User.Name的Display了

 

 

 

arrow
arrow
    全站熱搜

    軒軒的爸媽 發表在 痞客邦 留言(0) 人氣()