此论坛条目(http://stackoverflow.com/questions/2767557/wpf-get-property-that-a-control-is-bound-to-in-code-behind)提供了以下语句,以从"bound from“PropertyPath获取"bound to”DependencyProperty:
BindingExpression be = BindingOperations.GetBindingExpression((FrameworkElement)yourComboBox, ((DependencyProperty)Button.SelectedItemProperty));
string Name = be.ParentBinding.Path.Path; 我想更进一步--从那个PropertyPath中找到DependencyProperty。有什么标准的方法可以做到这一点吗?最终目标是在行为中使用它来删除现有绑定(AssociatedObject.PropertyPath到smth),并替换为两个绑定(Behavior.Original到smth和AssociatedObject.PropertyPath到Behavior.Modified)。
发布于 2013-12-18 21:17:29
您可以使用反射按名称获取依赖属性:
public static class ReflectionHelper
{
public static DependencyProperty GetDependencyProperty(this FrameworkElement fe, string propertyName)
{
var propertyNamesToCheck = new List<string> { propertyName, propertyName + "Property" };
var type = fe.GetType();
return (from propertyname in propertyNamesToCheck
select type.GetPublicStaticField(propertyname)
into field
where field != null
select field.GetFieldValue<DependencyProperty>(fe))
.FirstOrDefault();
}
public static FieldInfo GetPublicStaticField(this Type type, string fieldName)
{
return type.GetField(fieldName, BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static);
}
}https://stackoverflow.com/questions/12776370
复制相似问题