如何确定依赖项 属性 是否绑定到只读 属性?
How to find out if a dependency property is bound to a readonly property?
我定义了一个用户控件和一个依赖项属性(称为:文本)。
现在我想知道 Text 的绑定 属性 是否是只读的? (在后面的代码中)
我没有在 BindingExpression 中找到条目。
谁能帮帮我?
例如我们可以做这样的事情:
// create some control
var elem = new FrameworkElement();
// create context for control
elem.DataContext = new TestClass();
// create binding
var bind = elem.SetBinding(UIElement.AllowDropProperty, "ReadOnlyBool");
// we can resolve property
var pi = bind.ResolvedSource.GetType().GetProperty(bind.ResolvedSourcePropertyName);
// and check if it writeable
var isReadOnly = pi.CanWrite;
不是每个 BindingExpression 都有 ResolvedSource and/or ResolvedSourcePropertyName,所以,我想,这就是我们没有关于 resolved 属性.
的信息的原因
上下文:
public class TestClass : DependencyObject
{
public static readonly DependencyPropertyKey ReadOnlyBoolProperty =
DependencyProperty.RegisterReadOnly("ReadOnlyBool", typeof (bool), typeof (TestClass),
new PropertyMetadata());
public bool ReadOnlyBool => (bool) GetValue(ReadOnlyBoolProperty.DependencyProperty);
}
我定义了一个用户控件和一个依赖项属性(称为:文本)。 现在我想知道 Text 的绑定 属性 是否是只读的? (在后面的代码中)
我没有在 BindingExpression 中找到条目。
谁能帮帮我?
例如我们可以做这样的事情:
// create some control
var elem = new FrameworkElement();
// create context for control
elem.DataContext = new TestClass();
// create binding
var bind = elem.SetBinding(UIElement.AllowDropProperty, "ReadOnlyBool");
// we can resolve property
var pi = bind.ResolvedSource.GetType().GetProperty(bind.ResolvedSourcePropertyName);
// and check if it writeable
var isReadOnly = pi.CanWrite;
不是每个 BindingExpression 都有 ResolvedSource and/or ResolvedSourcePropertyName,所以,我想,这就是我们没有关于 resolved 属性.
的信息的原因上下文:
public class TestClass : DependencyObject
{
public static readonly DependencyPropertyKey ReadOnlyBoolProperty =
DependencyProperty.RegisterReadOnly("ReadOnlyBool", typeof (bool), typeof (TestClass),
new PropertyMetadata());
public bool ReadOnlyBool => (bool) GetValue(ReadOnlyBoolProperty.DependencyProperty);
}