如何创建只读依赖属性?

发布于 2024-07-26 09:38:47 字数 452 浏览 2 评论 0原文

如何创建只读依赖属性? 这样做的最佳实践是什么?

具体来说,最让我困惑的是,没有任何实现

DependencyObject.GetValue()  

将 System.Windows.DependencyPropertyKey 作为参数。

System.Windows.DependencyProperty.RegisterReadOnly 返回 DependencyPropertyKey 对象,而不是 DependencyProperty。 那么,如果无法调用 GetValue,该如何访问只读依赖属性呢? 或者您是否应该以某种方式将 DependencyPropertyKey 转换为普通的旧 DependencyProperty 对象?

建议和/或代码将不胜感激!

How do you create a read-only dependancy property? What are the best-practices for doing so?

Specifically, what's stumping me the most is the fact that there's no implementation of

DependencyObject.GetValue()  

that takes a System.Windows.DependencyPropertyKey as a parameter.

System.Windows.DependencyProperty.RegisterReadOnly returns a DependencyPropertyKey object rather than a DependencyProperty. So how are you supposed to access your read-only dependency property if you can't make any calls to GetValue? Or are you supposed to somehow convert the DependencyPropertyKey into a plain old DependencyProperty object?

Advice and/or code would be GREATLY appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一梦等七年七年为一梦 2024-08-02 09:38:47

实际上,这很简单(通过 RegisterReadOnly):

public class OwnerClass : DependencyObject // or DependencyObject inheritor
{
    private static readonly DependencyPropertyKey ReadOnlyPropPropertyKey
        = DependencyProperty.RegisterReadOnly(
            nameof(ReadOnlyProp),
            typeof(int), typeof(OwnerClass),
            new FrameworkPropertyMetadata(default(int),
                FrameworkPropertyMetadataOptions.None));

    public static readonly DependencyProperty ReadOnlyPropProperty
        = ReadOnlyPropPropertyKey.DependencyProperty;

    public int ReadOnlyProp
    {
        get { return (int)GetValue(ReadOnlyPropProperty); }
        protected set { SetValue(ReadOnlyPropPropertyKey, value); }
    }

    //your other code here ...
}

仅当您在私有/受保护/内部代码中设置值时才使用该密钥。 由于受保护的 ReadOnlyProp setter,这对您来说是透明的。

It's easy, actually (via RegisterReadOnly):

public class OwnerClass : DependencyObject // or DependencyObject inheritor
{
    private static readonly DependencyPropertyKey ReadOnlyPropPropertyKey
        = DependencyProperty.RegisterReadOnly(
            nameof(ReadOnlyProp),
            typeof(int), typeof(OwnerClass),
            new FrameworkPropertyMetadata(default(int),
                FrameworkPropertyMetadataOptions.None));

    public static readonly DependencyProperty ReadOnlyPropProperty
        = ReadOnlyPropPropertyKey.DependencyProperty;

    public int ReadOnlyProp
    {
        get { return (int)GetValue(ReadOnlyPropProperty); }
        protected set { SetValue(ReadOnlyPropPropertyKey, value); }
    }

    //your other code here ...
}

You use the key only when you set the value in private/protected/internal code. Due to the protected ReadOnlyProp setter, this is transparent to you.

寄与心 2024-08-02 09:38:47

我想指出的是,目前最好使用 https://github.com/HavenDV/DependencyPropertyGenerator< /a>,代码将非常简单:

[DependencyProperty<int>("ReadOnlyProperty", IsReadOnly = true)]
public partial class MyControl : UserControl
{
}

I want to note that at the current time it is better to use https://github.com/HavenDV/DependencyPropertyGenerator, the code will be extremely simple:

[DependencyProperty<int>("ReadOnlyProperty", IsReadOnly = true)]
public partial class MyControl : UserControl
{
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文