Adorning a non-DependencyObject with a DependencyProperty
I'm trying to bind to a WinForms control's DataSource property in my WPF application but I'开发者_Python百科m not having much luck...
Falafel.com gives it a shot, but although it claimes that a DependencyObject is not required to perform the binding, that seems to be the only way of doing it.
Has anyone done this before?
A. For objects not derived from DependencyObject
, you don't have access to SetBinding()
or ClearBinding()
methods. Instead you may call the static methods BindingOperations.SetBinding()
and BindingOperations.ClearBinding()
.
This is to actually bind a source and a target, and it can be done from any object using the static methods.
B. Now about the source and target involved in the binding, there are constraints that you can read here and that summarize to:
"The target of the binding can be any accessible property or element that is derived from DependencyProperty—an example is a TextBox control's Text property. The source of the binding can be any public property, including properties of other controls, common language runtime (CLR) objects, XAML elements, ADO.NET DataSets, XML Fragments, and so forth."
In turn DependencyProperty
can exist (if I'm not wrong) only in a DependencyObject
. So the target needs to live in a DependencyObject
, but not the source, and not the object from where the binding is created.
And this is not exactly true, you can also use a target that is not a DO, look at the code in this page.
- Also remember that you can perform a
reverse binding using
OneWayToSource
type of binding. In this case the target has not to be aDependencyObject
.
精彩评论