wpf 绑定到属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12492867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding to attributes
提问by torpederos
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.
是否有可能将描述属性绑定到标签内容。我正在寻找的解决方案不应该需要实例化 Class 对象。
采纳答案by H.B.
It won't be a proper binding (which is not necessary for static data anyway) but you can easily create a MarkupExtensionto retrieve it, just pass the type and the property name and get it via reflection.
它不会是一个正确的绑定(无论如何这对于静态数据来说不是必需的)但是您可以轻松创建一个MarkupExtension来检索它,只需传递类型和属性名称并通过反射获取它。
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}"
回答by Aghilas Yakoub
1 You create an Converter
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 You insert your ressource
2 你插入你的资源
<Window.Resources>
<local:PropertyDescriptionConverter x:Key="PropertyDescriptionConverter" />
</Window.Resources>
3 Add this binding
3 添加这个绑定
"{Binding ConverterParameter=Name, Converter={StaticResource PropertyDescriptionConverter}}"
回答by Felice Pollano
You can't since it is an metadata of the property. You can workaround by creating a custom binding class.
你不能,因为它是属性的元数据。您可以通过创建自定义绑定类来解决。
回答by DHN
Well then create a reader class, which reads the attributes of the class and bind the attributes of the reader class. E.g.
那么创建一个reader类,读取类的属性,绑定reader类的属性。例如
public class Reader
{
public Dictionary<string, string> Description {get; set;}
}
