本文介绍了绑定到属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我上课
public class Car { [Description("name of the car")] public string Name { get; set; } [Description("age of the car")] public int Age { get; set; } }是否有可能将Description属性绑定到Label内容.我正在寻找的解决方案不需要实例化Car对象.
Is there any possibility to bind Description attribute to Label content. The solution what I'm looking for shouldn't require to instantiate Car object.
推荐答案这不是一个适当的绑定(无论如何对于静态数据来说都是不必要的),但是您可以轻松地创建 MarkupExtension 进行检索,只需传递类型和属性名称并获取它通过反射.
It won't be a proper binding (which is not necessary for static data anyway) but you can easily create a MarkupExtension to retrieve it, just pass the type and the property name and get it via reflection.
概述如下:
public Type Type { get; set; } public string PropertyName { get; set; } ProvideValue: Type.GetProperty(PropertyName) .GetCustomAttributes(true) .OfType<DescriptionAttribute>() .First() .Description <!-- Usage example --> Text="{me:Description Type=local:Car, PropertyName=Name}"更多推荐
绑定到属性
发布评论