`

Readonly Dependency Property

    博客分类:
  • WPF
wpf 
阅读更多

If you are familiar with the Dependency Property system in WPF, you may find that create a Dependency Property that is supposed to be readonly is a bit different from the create a normal Dependency Property.

 

From this part on, I will use DP as a short name for Dependency Property.

 

 

The major difference is that when you create a readonly DP , you will use RegisterReadonly method as below.

 

public static DependencyPropertyKey RegisterReadOnly(
	string name,
	Type propertyType,
	Type ownerType,
	PropertyMetadata typeMetadata
)

 

 

It is a static method of DependencyProperty class. 

 

the return type of RegisterReadonly method is DependencyPropertyKey, which provide limited write access to read-only dependency property.

 

 

To get the value dp created by DependencyPropertyKey, you may do something like this :

 

 

public static readonly DependencyProperty ReadonlyProperty =
  ReadonlyPropertyKey.DependencyProperty;
public double Readonly
{
  get { return (double)GetValue(ReadonlyProperty); }
}
 

in the code above, the ReadonlyPropertyKey is the key to registered dp.

 

As remarked by "RegisterReadonly" method, which I quoted as below.

 

 

RegisterReadonly Remarks 写道
This method returns the type DependencyPropertyKey, whereas RegisterAttached returns the type DependencyProperty. Typically, the keys that represent read-only properties are not made public, because the keys can be used to set the dependency property value by calling SetValue(DependencyPropertyKey, Object). Your class design will affect your requirements, but it is generally recommended to limit the access and visibility of any DependencyPropertyKey to only those parts of your code that are necessary to set that dependency property as part of class or application logic. It is also recommended that you expose a dependency property identifier for the read-only dependency property, by exposing the value of DependencyPropertyKey.DependencyProperty as a public static readonly field on your class.

Read-only dependency properties are a fairly typical scenario both in the existing API and for customization scenarios, because other WPF features might require a dependency property even if that property is not intended to be settable by callers. You can use the value of a read-only dependency property as the basis for other property system operations that take a dependency property, such as basing a Trigger on the dependency property in a style.

For more information on dependency property registration, see DependencyProperty.
 

 

You may want to 

1. make internal or private access on the keys

2. expose (make public) the DependencyProperty (rather than the DependencypropertyKey which can be used to set the value of dp) 

3. it is common practise to only provide a read accesor (do not provide a write accessor), or provide a restricted 

 

so, for example, 

it is normal you create a dpKey as follow

 

/* internal or private access to the key*/
internal static readonly dependencyPropertyKey ReadonlyKey = DependencyProperty.RegisterReadonly("Reaonly", typeof(string), type(OwnerClass), new PropertyMetadata(string.Empty));
 

and you will expose the DependencyProperty as follow.

 

 

ublic static readonly DependencyProperty ReadOnlyProperty
        = ReadOnlyPropertyKey.DependencyProperty;
 

and then you might provide the accessor with appropriate protect level as follow.

 

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

 

If you have registered some PropertyMetadata, it might look like this : 

 

 private static void OnReadOnlyPropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((OwnerClass)d).OnReadOnlyPropChanged(e);
    }
 

 

In the stackover flow post, it has the following example showing you how do you normally organize the readonly dp.

 

private static readonly DependencyPropertyKey ReadOnlyPropPropertyKey
        = DependencyProperty.RegisterReadOnly("ReadOnlyProp", typeof(int), typeof(OwnerClass),
            new FrameworkPropertyMetadata((int)0,
                FrameworkPropertyMetadataOptions.None,
                new PropertyChangedCallback(OnReadOnlyPropChanged)));

    public static readonly DependencyProperty ReadOnlyPropProperty
        = ReadOnlyPropPropertyKey.DependencyProperty;

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

    private static void OnReadOnlyPropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((OwnerClass)d).OnReadOnlyPropChanged(e);
    }
 

 

 

分享到:
评论

相关推荐

    WPF源码:依赖属性回调、验证及强制值.rar

    它提供了丰富的用户界面元素和设计模式,其中包括依赖属性(Dependency Property)系统。本篇将深入探讨依赖属性的回调、验证和强制值这三个核心概念,并通过一个名为"WpfApplication2"的项目源码进行详细阐述。** ##...

    Spring 事务代理配置

    其中,依赖注入(Dependency Injection,简称DI)和面向切面编程(Aspect-Oriented Programming,简称AOP)是其两大核心特性,而事务管理则是AOP的一个典型应用场景。 ### Spring事务管理的核心组件 Spring的事务...

    DependencySample

    在.NET框架中,WPF(Windows Presentation Foundation)提供了一种强大的数据绑定机制,其中的核心组件之一就是依赖属性(Dependency Property)。本示例"DependencySample"旨在深入讲解如何自定义依赖属性,这对于...

    在Canvas上根据变量改变Shape的位置?

    当你想要根据变量来动态调整Shape(如Rectangle、Ellipse等)的位置时,你可以利用数据绑定(Data Binding)和依赖属性(Dependency Property)来实现这一功能。 首先,理解数据绑定是WPF中的一个核心概念。数据...

    WPF 依赖属性和附加属性(定义和使用)

    在Windows Presentation Foundation(WPF)中,依赖属性(Dependency Property)和附加属性(Attached Property)是两个核心概念,它们为数据绑定、属性继承以及自定义控件提供了强大的支持。理解并熟练运用这两个...

    TreeView+MVVM实现“多选”

    2. 依赖属性(Dependency Property): 在WPF中,依赖属性是实现数据绑定和动画的关键。它们是属性的一种特殊形式,允许控件和其他组件在运行时动态更改其值。依赖属性提供了变更通知、数据验证、默认值等功能,...

    WPF 使用MVVM模式 关闭窗口

    3. **依赖属性(Dependency Property)**: ViewModel可以定义一个依赖属性,如`CloseRequested`,当ViewModel需要关闭窗口时,改变这个属性的值。View可以通过数据绑定监听这个属性的变化,当属性值改变时执行关闭...

    依赖属性实现绑定

    在WPF(Windows Presentation Foundation)开发中,依赖属性(Dependency Property)是实现数据绑定的关键机制。依赖属性允许控件的属性与其他对象的数据源保持同步,从而实现数据驱动的用户界面。接下来,我们将...

    自己平时练手的代码,依赖属性和路由事件

    在IT行业中,依赖属性(Dependency Property)是一种在WPF(Windows Presentation Foundation)和UWP(Universal Windows Platform)框架中广泛使用的特性,它允许组件之间共享数据并实现动态更新。依赖属性提供了一...

    WpfApplication5_1

    在WPF中,路由事件经常与依赖属性(Dependency Property)结合使用。依赖属性允许属性值在UI元素树中绑定和传播,而路由事件则处理这些属性的改变。通过`OnPropertyChangedCallback`回调,我们可以关联一个路由事件...

    wpf 用户控件实现分页

    同时,我们还需要实现依赖属性(Dependency Property)来绑定当前页数和总页数,以便在UI中实时更新。 ```csharp public partial class MyPagingControl : UserControl { public static readonly ...

    WPF下自定义时间格式

    1. **依赖属性(Dependency Property)**:依赖属性是 WPF 中用于支持属性系统的基础,它能够高效地处理数据绑定和属性动画等特性。 2. **日历控件(Calendar Control)**:WPF 提供的日历控件可以用来选择或展示...

    MyBatis3.2.6

    <cache eviction="LRU" flushInterval="60000" size="512" readOnly="true"/> ``` ##### 4.2 插件扩展 MyBatis 允许通过插件机制扩展其功能。插件可以拦截 SQL 执行过程中的各个阶段,如参数处理、结果处理等。 *...

    探索用户控件中依赖项属性的使用

    依赖项属性(Dependency Property)是WPF中核心特性之一,它们为控件提供数据绑定、动画、样式和模板等高级功能。本文将深入探讨在用户控件中如何有效利用依赖项属性。 一、依赖项属性概述 依赖项属性是一种特殊的...

    Java调用Google Analytics API实现网站统计demo

    .setProperty("properties/your_property_id") // 替换为你的Google Analytics Property ID .addDimensions(new Dimension().setName("ga:sourceMedium")) // 按源/媒介分组数据 .addMetrics(new Metric().set...

    SpringNet 依赖注入示例代码

    private readonly IService service; public ServiceConsumer(IService service) { this.service = service; } } ``` 2. 属性注入:通过设置类的公共属性来注入依赖对象。在配置文件中,可以指定属性的名称和...

    在WPF中利用IoC

    接下来,在视图模型中,我们可以利用依赖属性(Dependency Property)或构造函数注入来获取服务: ```csharp public class MainViewModel : INotifyPropertyChanged { private readonly IMyDataService _data...

    gdalJAVA配置dll和jar

    System.setProperty("java.library.path", "C:\\path\\to\\gdal\\dlls"); System.loadLibrary("gdal"); ``` 3. **测试配置**:配置完成后,可以编写简单的Java代码来验证GDAL是否能正常工作。例如,你可以尝试...

    Spring .Net 控制反转

    Spring .Net 是一个开源框架,它是针对 .Net 平台实现的 IoC 容器,它允许开发者通过依赖注入(Dependency Injection,DI)来实现 IoC。本文将深入探讨 Spring .Net 中的 IoC 和 DI 知识点,并提供实例说明如何在...

Global site tag (gtag.js) - Google Analytics