绑定到属性

编程入门 行业动态 更新时间:2024-10-22 21:23:36
本文介绍了绑定到属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我上课

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}"

更多推荐

绑定到属性

本文发布于:2023-11-12 12:30:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581503.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   属性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!