WPF-从绑定路径获取属性值
如果我有一个名为MyObject的对象,它有一个名为MyChild的属性,它本身有一个名为Name的属性。如果我只有一个绑定路径(即“MyChild.Name”)和对MyObject的引用,那么如何获取该Name属性的值WPF-从绑定路径获取属性值,wpf,binding,markup-extensions,Wpf,Binding,Markup Extensions,如果我有一个名为MyObject的对象,它有一个名为MyChild的属性,它本身有一个名为Name的属性。如果我只有一个绑定路径(即“MyChild.Name”)和对MyObject的引用,那么如何获取该Name属性的值 MyObject -MyChild -Name 不确定您想做什么,但知道如何命名(xaml或代码),但始终可以命名对象 <MyObject x:Name="myBindingObject" ... /> 或者在xaml中 <BeginStory
MyObject
-MyChild
-Name
不确定您想做什么,但知道如何命名(xaml或代码),但始终可以命名对象
<MyObject x:Name="myBindingObject" ... />
或者在xaml中
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="myBindingObject"
Storyboard.TargetProperty="Background"
To="AA2343434" Duration="0:0:2" >
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
我找到了一种方法,但它很难看,而且可能不是很快。。。基本上,其思想是创建具有给定路径的绑定,并将其应用于依赖项对象的属性。这样,绑定就完成了检索值的所有工作:
public static class PropertyPathHelper
{
public static object GetValue(object obj, string propertyPath)
{
Binding binding = new Binding(propertyPath);
binding.Mode = BindingMode.OneTime;
binding.Source = obj;
BindingOperations.SetBinding(_dummy, Dummy.ValueProperty, binding);
return _dummy.GetValue(Dummy.ValueProperty);
}
private static readonly Dummy _dummy = new Dummy();
private class Dummy : DependencyObject
{
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(object), typeof(Dummy), new UIPropertyMetadata(null));
}
}
我开发了一个能满足你需要的软件
它包含一个类Resolver
,该类有一个Resolve
方法,其行为类似于@ThomasLevesque的GetValue
方法。示例:
IResolver resolver = new Resolver();
var o = new { Property1 = Property2 = "value" } };
var path = "Property1.Property2";
object result = r.Resolve(o, path); //the result is the string "value"
它甚至支持通过索引进行收藏访问,或通过键进行字典访问。这些路径的示例包括:
"ArrayProperty[5]"
"DictionaryProperty[Key]"
我是这样做的。请让我知道这是否是一个糟糕的想法,因为C#对我来说只是一个副业,所以我不是专家反对添加类型为ItemsControl:
BindingExpression itemsSourceExpression = GetaBindingExression(objectToAddTo);
object itemsSourceObject = (object)itemsSourceExpression.ResolvedSource;
string itemSourceProperty = itemsSourceExpression.ResolvedSourcePropertyName;
object propertyValue = itemsSourceObject.GetType().GetProperty(itemSourceProperty).GetGetMethod().Invoke(itemsSourceObject, null); // Get the value of the property
你能举个例子说明你想如何使用它吗?这似乎需要很多机器来获得绑定的价值,但我想不出更好的通用代码解决方案+1和干杯。太好了,谢谢托马斯!我之所以需要这样的东西,是因为我编写了一个用于加载图像的自定义标记扩展。这个扩展有一个图像名称的Name属性,我现在想将它绑定到使用标记扩展的DataTemplate中的model属性。但是,我不能绑定,因为Name不是DP,它也不能:(这是我能想到的唯一解决方案,所以我将尝试一下这段代码。谢谢。这非常适合我的需要(获取和设置给定绑定路径的视图模型属性的值)。有没有类似的方法来获取将与给定绑定路径绑定的属性的类型?@Wayne,我不知道。您可以使用上面的技巧获取值,并对其调用GetType,但如果该值为null,它当然不起作用……对,问题是null。在这种情况下,我仍然需要该类型。至于refle但是,考虑到绑定路径对属性访问的使用不仅仅是点符号(例如,[]对列表和字典进行索引),这种逻辑可能很复杂。我希望BindingOperations等的其他隐藏功能也能帮我。谢谢。它支持访问显式实现的接口属性和隐藏属性吗?@Grx70是的。它通过反射工作,因此如果该属性存在于对象上并且是公共的,它将获得其值。
BindingExpression itemsSourceExpression = GetaBindingExression(objectToAddTo);
object itemsSourceObject = (object)itemsSourceExpression.ResolvedSource;
string itemSourceProperty = itemsSourceExpression.ResolvedSourcePropertyName;
object propertyValue = itemsSourceObject.GetType().GetProperty(itemSourceProperty).GetGetMethod().Invoke(itemsSourceObject, null); // Get the value of the property