Reputation: 845
I´m looking for a way to check in code behind, if a property of a control has been bound and can´t seem to find the right way to do it. I think I need to get the FieldInfo first (using System.Windows.Controls.Control control in a generic method):
FieldInfo te = null;
foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(control))
{
if (prop.Name.Equals("Visibility"))
{
te = control.GetType().GetField(prop.Name + "Property");
break;
}
}
...does find the PropertyDescriptor, but not the FieldInfo.
FieldInfo gg = control.GetType().GetField("Visibility", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
FieldInfo gg1 = control.GetType().GetField("VisibilityProperty", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
FieldInfo gg2 = typeof(Control).GetField("Visibility", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
FieldInfo gg3 = typeof(Control).GetField("VisibilityProperty", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
...all return null as FieldInfo. When the FieldInfo has been found I would like to check if the value has been bound by doing:
DependencyProperty dp = (DependencyProperty)field.GetValue(control);
if (control.GetBindingExpression(dp) == null) ...
Any ideas?
Upvotes: 1
Views: 1490
Reputation: 2868
You can try using BindingOperations.GetBinding
to get Binding Object. Like,
// textBox3 is an instance of a TextBox
// the TextProperty is the data-bound dependency property
Binding myBinding = BindingOperations.GetBinding(textBox3, TextBox.TextProperty);
Ref. How to: Get the Binding Object from a Bound Target Property
Upvotes: 2