- 浏览: 401248 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
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.
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 - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1979first let’s see an example ... -
WPF – Virtualization – VirutalizationStackPanel and ItemsPanelTemplate
2013-08-05 21:55 1423Topic: WPF – Virtualization – ... -
wpf - BehaviorBase and one use examples
2013-06-18 18:41 1319Behavior is something that we ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1926You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5337As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1191Recently I was trying to debug ... -
wpf - default implicit style
2013-05-10 10:24 796We know that if you left out ... -
wpf - Style setter on the attached property
2013-05-08 14:54 2859I believe that you are familia ... -
wpf - specify enum values in xaml
2013-05-08 11:31 3594Many a situation you find tha ... -
wpf - IG xamDataGrid bind to XmlDataProvider with Xml Island
2012-12-18 14:28 1290Sometimes you may bind to some ... -
wpf - translate winform button/mouse event to wpf events
2012-12-12 17:37 2166It is common that we sometimes ... -
wpf - Freezable and its meaning
2012-09-27 12:38 0This article is based on the di ... -
wpf - Customize the grid lines for original wpf Grid control
2012-09-27 12:01 1461The System.WIndows.Controls.Gri ... -
c# - Convert from System.Drawing.Image to System.WIndows.Media.ImageSource
2012-09-25 14:27 7421In Previous discussion, we have ... -
wpf - Get Effective DependencyProperty value on a DependencyObject
2012-08-28 19:05 1048As discussed in my previous pos ... -
wpf - Default(Theme) style and its DefaultStyleKey
2012-08-28 17:54 1392As dicsused in the subsection o ... -
wpf - Dependency Property Value Precedence
2012-08-28 18:56 886A dependency property to an Dep ... -
wpf - WPF TemplateBinding vs RelativeSource TemplatedParent
2012-08-28 14:20 3720This is a post that summarizes ... -
wpf - ICutomTypeDescriptor , PropertyDescriptor and its use in PropertyGrid
2012-08-28 14:04 3589The type ICustomTypeDe ... -
wpf - tips to convert UI controls in WPF/Silverlight/Winforms into a Bitmap
2012-08-27 17:44 987In previous discussion, we have ...
相关推荐
在WPF中,依赖属性(Dependency Property)是实现数据绑定(Data Binding)的基础之一。本文将围绕依赖属性在WPF中的作用及其实现机制进行深入探讨。 #### 二、依赖属性概述 依赖属性是WPF中一种特殊类型的属性,...
它提供了丰富的用户界面元素和设计模式,其中包括依赖属性(Dependency Property)系统。本篇将深入探讨依赖属性的回调、验证和强制值这三个核心概念,并通过一个名为"WpfApplication2"的项目源码进行详细阐述。** ##...
其中,依赖注入(Dependency Injection,简称DI)和面向切面编程(Aspect-Oriented Programming,简称AOP)是其两大核心特性,而事务管理则是AOP的一个典型应用场景。 ### Spring事务管理的核心组件 Spring的事务...
在.NET框架中,WPF(Windows Presentation Foundation)提供了一种强大的数据绑定机制,其中的核心组件之一就是依赖属性(Dependency Property)。本示例"DependencySample"旨在深入讲解如何自定义依赖属性,这对于...
当你想要根据变量来动态调整Shape(如Rectangle、Ellipse等)的位置时,你可以利用数据绑定(Data Binding)和依赖属性(Dependency Property)来实现这一功能。 首先,理解数据绑定是WPF中的一个核心概念。数据...
在Windows Presentation Foundation(WPF)中,依赖属性(Dependency Property)和附加属性(Attached Property)是两个核心概念,它们为数据绑定、属性继承以及自定义控件提供了强大的支持。理解并熟练运用这两个...
2. 依赖属性(Dependency Property): 在WPF中,依赖属性是实现数据绑定和动画的关键。它们是属性的一种特殊形式,允许控件和其他组件在运行时动态更改其值。依赖属性提供了变更通知、数据验证、默认值等功能,...
3. **依赖属性(Dependency Property)**: ViewModel可以定义一个依赖属性,如`CloseRequested`,当ViewModel需要关闭窗口时,改变这个属性的值。View可以通过数据绑定监听这个属性的变化,当属性值改变时执行关闭...
在WPF(Windows Presentation Foundation)开发中,依赖属性(Dependency Property)是实现数据绑定的关键机制。依赖属性允许控件的属性与其他对象的数据源保持同步,从而实现数据驱动的用户界面。接下来,我们将...
在IT行业中,依赖属性(Dependency Property)是一种在WPF(Windows Presentation Foundation)和UWP(Universal Windows Platform)框架中广泛使用的特性,它允许组件之间共享数据并实现动态更新。依赖属性提供了一...
在WPF中,路由事件经常与依赖属性(Dependency Property)结合使用。依赖属性允许属性值在UI元素树中绑定和传播,而路由事件则处理这些属性的改变。通过`OnPropertyChangedCallback`回调,我们可以关联一个路由事件...
同时,我们还需要实现依赖属性(Dependency Property)来绑定当前页数和总页数,以便在UI中实时更新。 ```csharp public partial class MyPagingControl : UserControl { public static readonly ...
1. **依赖属性(Dependency Property)**:依赖属性是 WPF 中用于支持属性系统的基础,它能够高效地处理数据绑定和属性动画等特性。 2. **日历控件(Calendar Control)**:WPF 提供的日历控件可以用来选择或展示...
<cache eviction="LRU" flushInterval="60000" size="512" readOnly="true"/> ``` ##### 4.2 插件扩展 MyBatis 允许通过插件机制扩展其功能。插件可以拦截 SQL 执行过程中的各个阶段,如参数处理、结果处理等。 *...
依赖项属性(Dependency Property)是WPF中核心特性之一,它们为控件提供数据绑定、动画、样式和模板等高级功能。本文将深入探讨在用户控件中如何有效利用依赖项属性。 一、依赖项属性概述 依赖项属性是一种特殊的...
.setProperty("properties/your_property_id") // 替换为你的Google Analytics Property ID .addDimensions(new Dimension().setName("ga:sourceMedium")) // 按源/媒介分组数据 .addMetrics(new Metric().set...
private readonly IService service; public ServiceConsumer(IService service) { this.service = service; } } ``` 2. 属性注入:通过设置类的公共属性来注入依赖对象。在配置文件中,可以指定属性的名称和...
接下来,在视图模型中,我们可以利用依赖属性(Dependency Property)或构造函数注入来获取服务: ```csharp public class MainViewModel : INotifyPropertyChanged { private readonly IMyDataService _data...
System.setProperty("java.library.path", "C:\\path\\to\\gdal\\dlls"); System.loadLibrary("gdal"); ``` 3. **测试配置**:配置完成后,可以编写简单的Java代码来验证GDAL是否能正常工作。例如,你可以尝试...
Spring .Net 是一个开源框架,它是针对 .Net 平台实现的 IoC 容器,它允许开发者通过依赖注入(Dependency Injection,DI)来实现 IoC。本文将深入探讨 Spring .Net 中的 IoC 和 DI 知识点,并提供实例说明如何在...