绑定到属性

时间:2012-09-19 10:27:29

标签: c# wpf xaml data-binding

我有课

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内容。我正在寻找的解决方案不应该要求实例化Class对象。

5 个答案:

答案 0 :(得分:9)

它不是一个正确的绑定(无论如何都不是静态数据所必需的)但你可以轻松创建一个MarkupExtension来检索它,只需传递类型和属性名称并通过反射得到它。

大纲将是这样的:

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

答案 1 :(得分:1)

你不能,因为它是属性的元数据。您可以通过创建自定义绑定类来解决此问题。

答案 2 :(得分:1)

1您创建了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您插入资源

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

3添加此绑定

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

答案 3 :(得分:1)

使用字符串常量代替字符串字面量来设置属性参数:

Sub Caller()
    Dim c As Color = GetLastColor(
        SomeBmp,
        Color.FromArgb(255, 0, 0),
        Color.FromArgb(0, 255, 0),
        Color.FromArgb(0, 0, 255))

    ' TODO: ...
End Sub

可以通过 public class Car { public const string CarNamePropertyDescription = "name of the car"; [Description(CarNamePropertyDescription)] public string Name { get; set; } } 扩展从 xaml 访问常量(不需要绑定,因为属性不会在运行时更改):

{x:Static}

答案 4 :(得分:0)

然后创建一个reader类,它读取类的属性并绑定reader类的属性。 E.g。

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