C# 绑定到属性

C# 绑定到属性,c#,wpf,xaml,data-binding,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; } } 是否可以将描述属性绑定到标签内容。我正在寻找的解决方案不需要实例化Car对象。您不能,因为它是属性的元数据。您可以

我有一节课

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

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

是否可以将描述属性绑定到标签内容。我正在寻找的解决方案不需要实例化Car对象。

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

它不是一个正确的绑定(静态数据无论如何都不需要),但您可以轻松创建一个绑定来检索它,只需传递类型和属性名,然后通过反射来获取

大纲应该是这样的:

public Type Type { get; set; }
public string PropertyName { get; set; }

ProvideValue: Type.GetProperty(PropertyName)
                  .GetCustomAttributes(true)
                  .OfType<DescriptionAttribute>()
                  .First()
                  .Description
public类型{get;set;}
公共字符串PropertyName{get;set;}
ProviderValue:Type.GetProperty(PropertyName)
.GetCustomAttributes(true)
第()类
.First()
描述

Text=“{me:Description Type=local:Car,PropertyName=Name}”

1您创建了一个
转换器

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>

然后创建一个reader类,它读取类的属性并绑定reader类的属性。例如

公共类读取器
{
公共字典说明{get;set;}

}

使用字符串常量设置属性参数,而不是字符串文字:

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



您不应该这样做,这是一种填充UI元素的丑陋方式。你到底想实现什么-让一个窗口根据它绑定到的内容自动填充?是的,我想让窗口中的所有标签从类属性属性“Description”中获取它们的内容。此属性也用于验证目的。您应该将标签绑定到字符串资源文件中的文本。如果绑定项可以更改,请使用DataTemplates。问题明确指出没有实例。我很矛盾-你正在向他展示一种方法,但你也让他能够创建属于魔多深处的代码…@slugster:我不知道,也有人使用本地化的
DisplayName
属性,你能解释一下为什么你会有这样的问题吗?本地化似乎与OP的想法相去甚远,我怀疑他这样做是因为他避免或不知道数据模板。在对大局没有任何解释的情况下,我感觉到一场即将发生的火车失事。@slugster:在对问题的评论中提到了“标签”,所以如果它只用于这个问题,而不用于实际数据,它应该绑定到标签旁边的某个控件上,那么应该可以。。。
public class Reader
{
   public Dictionary<string, string> Description {get; set;}
}
public class Car
{
    public const string CarNamePropertyDescription = "name of the car";
    
    [Description(CarNamePropertyDescription)]
    public string Name { get; set; }
}
<Label Content="{x:Static namespace:Car.CarNamePropertyDescription}"/>