Binding to attributes

假如想象 提交于 2021-02-16 04:46:27

问题


I have class

public class Car {

    [Description("name of the car")]
    public string Name { get; set; }

    [Description("age of the car")]
    public int Age { get; set; }
}

is there any possibility to bind Description attribute to Label content. The solution what I'm looking for shouldn't require to instantiate Class object.


回答1:


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.

Outline would be something like:

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



回答2:


You can't since it is an metadata of the property. You can workaround by creating a custom binding class.




回答3:


1 You create an Converter

public sealed class PropertyDescriptionConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return Binding.DoNothing;

            string propertyName = parameter as string;
            if (String.IsNullOrEmpty(propertyName))
                return new ArgumentNullException("parameter").ToString();

            Type type = value.GetType();

            PropertyInfo property = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);
            if (property == null)
                return new ArgumentOutOfRangeException("parameter", parameter,
                    "Property \"" + propertyName + "\" not found in type \"" + type.Name + "\".").ToString();

            if (!property.IsDefined(typeof(DescriptionAttribute), true))
                return new ArgumentOutOfRangeException("parameter", parameter,
                    "Property \"" + propertyName + "\" of type \"" + type.Name + "\"" +
                    " has no associated Description attribute.").ToString();

            return ((DescriptionAttribute)property.GetCustomAttributes(typeof(DescriptionAttribute), true)[0]).Description;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

2 You insert your ressource

    <Window.Resources>
        <local:PropertyDescriptionConverter x:Key="PropertyDescriptionConverter" />
    </Window.Resources>

3 Add this binding

"{Binding ConverterParameter=Name, Converter={StaticResource PropertyDescriptionConverter}}"



回答4:


Well then create a reader class, which reads the attributes of the class and bind the attributes of the reader class. E.g.

public class Reader
{
   public Dictionary<string, string> Description {get; set;}
}


来源:https://stackoverflow.com/questions/12492867/binding-to-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!