我正在寻找一种方法来检查后面的代码,如果控件的属性已被绑定并且似乎无法找到正确的方法来做到这一点。我想我需要先获取 FieldInfo(在通用方法中使用 System.Windows.Controls.Control 控件):FieldInfo te = null;foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(control)){ if (prop.Name.Equals("Visibility")) { te = control.GetType().GetField(prop.Name + "Property"); break; }}...确实找到了 PropertyDescriptor,但没有找到 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);...都返回 null 作为 FieldInfo。找到 FieldInfo 后,我想通过执行以下操作来检查该值是否已绑定:DependencyProperty dp = (DependencyProperty)field.GetValue(control);if (control.GetBindingExpression(dp) == null) ...有任何想法吗?
1 回答
当年话下
TA贡献1890条经验 获得超9个赞
您可以尝试使用BindingOperations.GetBinding来获取绑定对象。喜欢,
// textBox3 is an instance of a TextBox
// the TextProperty is the data-bound dependency property
Binding myBinding = BindingOperations.GetBinding(textBox3, TextBox.TextProperty);
- 1 回答
- 0 关注
- 143 浏览
添加回答
举报
0/150
提交
取消