开发者

WPF: Find bindings domain object property

Is there a simple way to determine whether a property from a domain object is bound to a WPF element, and if so, which element and property?

For example, lets say that I have a UserControl with a TextBox and I bind the "Text" property of the control to the "Description" property of my domain object. Given onl开发者_StackOverflow中文版y the name of the domain object property ("Description"), how do I retrieve the BindingExpression for that property?


No, there is no simple way to do this. Binding doesn't work that, your Property can be bound to multiple Dependency Properties of multiple elements and have no idea about it. You would basically have to traverse the VisualTree, enumerating Dependency Properties for each FrameworkElement, get BindingExpressions and compare the Path for the Binding to see if it matches the property name you're searching for. On top of this, you'll also have to compare the DataContext to the object. I can still think of many situations were this will fail, e.g. Path is not always used in a binding, you can use the DataContext directly.

Anyway, here is a link with an implementation that actually does this. I tried it out and it works but it took about 2 seconds to complete a search with just a DataGrid containing 4 columns and 10 rows. It doesn't do the DataContext check though but I added that to my test-app but I won't post the code since I don't think you'll be using this. Let me know if you want it anyway and I'll post it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜