【问题标题】:How to find out if a dependency property is bound to a readonly property?如何确定依赖属性是否绑定到只读属性?
【发布时间】:2015-11-16 17:04:52
【问题描述】:

我定义了一个用户控件和一个依赖属性(称为:文本)。 现在我想知道 Text 的绑定属性是否是只读的? (在后面的代码中)

我在 BindingExpression 中没有找到条目。

谁能帮帮我?

【问题讨论】:

  • 您将无法创建到只读属性的 TwoWay 或 OneWayToSource 绑定。那你的意思是什么?
  • 我想找出它来设置内部TextEdit的只读标志。
  • 如果您有想法解决我的其他问题,请提供帮助。
  • 它没有帮助。在双向绑定的情况下,模式是默认的。

标签: wpf binding dependency-properties


【解决方案1】:

例如,我们可以做这样的事情:

// 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 和/或 ResolvedSourcePropertyName,所以,我想,这就是我们没有关于已解析属性的信息的原因。

上下文:

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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 2023-03-26
    • 2018-05-21
    • 2012-08-29
    • 2020-12-22
    • 1970-01-01
    • 2020-08-06
    相关资源
    最近更新 更多