DependencyProperty.RegisterReadOnly 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将依赖属性注册为只读依赖属性。
重载
| 名称 | 说明 |
|---|---|
| RegisterReadOnly(String, Type, Type, PropertyMetadata) |
使用指定的属性类型、所有者类型和属性元数据注册只读依赖属性。 |
| RegisterReadOnly(String, Type, Type, PropertyMetadata, ValidateValueCallback) |
使用指定的属性类型、所有者类型、属性元数据和验证回调注册只读依赖属性。 |
RegisterReadOnly(String, Type, Type, PropertyMetadata)
使用指定的属性类型、所有者类型和属性元数据注册只读依赖属性。
public:
static System::Windows::DependencyPropertyKey ^ RegisterReadOnly(System::String ^ name, Type ^ propertyType, Type ^ ownerType, System::Windows::PropertyMetadata ^ typeMetadata);
public static System.Windows.DependencyPropertyKey RegisterReadOnly(string name, Type propertyType, Type ownerType, System.Windows.PropertyMetadata typeMetadata);
static member RegisterReadOnly : string * Type * Type * System.Windows.PropertyMetadata -> System.Windows.DependencyPropertyKey
Public Shared Function RegisterReadOnly (name As String, propertyType As Type, ownerType As Type, typeMetadata As PropertyMetadata) As DependencyPropertyKey
参数
- name
- String
要注册的依赖属性的名称。
- propertyType
- Type
属性类型。
- ownerType
- Type
正在注册依赖属性的所有者类型。
- typeMetadata
- PropertyMetadata
依赖属性的属性元数据。
返回
一个依赖属性键,该键应用于在类中设置静态只读字段的值,该字段随后用于引用依赖属性。
示例
以下示例将 AquariumSize 依赖属性注册为只读。 该示例定义为AquariumSizeKey内部键(以便程序集中的其他类可以重写元数据),并根据该键公开依赖属性标识符。AquariumSizeProperty 此外,会创建一个包装器,只使用 AquariumSizeget 访问器。
internal static readonly DependencyPropertyKey AquariumSizeKey = DependencyProperty.RegisterReadOnly(
"AquariumSize",
typeof(double),
typeof(Aquarium),
new PropertyMetadata(double.NaN)
);
public static readonly DependencyProperty AquariumSizeProperty =
AquariumSizeKey.DependencyProperty;
public double AquariumSize
{
get { return (double)GetValue(AquariumSizeProperty); }
}
Friend Shared ReadOnly AquariumSizeKey As DependencyPropertyKey = DependencyProperty.RegisterReadOnly("AquariumSize", GetType(Double), GetType(Aquarium), New PropertyMetadata(Double.NaN))
Public Shared ReadOnly AquariumSizeProperty As DependencyProperty = AquariumSizeKey.DependencyProperty
Public ReadOnly Property AquariumSize() As Double
Get
Return CDbl(GetValue(AquariumSizeProperty))
End Get
End Property
注解
此方法返回类型 DependencyPropertyKey,而 RegisterAttached 返回类型 DependencyProperty。 通常,表示只读属性的键不是公开的,因为这些键可用于通过调用 SetValue(DependencyPropertyKey, Object)来设置依赖属性值。 类设计会影响你的要求,但通常建议将任何对象的 DependencyPropertyKey 访问和可见性限制为仅将依赖属性设置为类或应用程序逻辑的一部分所需的代码部分。 此外,建议通过公开类DependencyPropertyKey.DependencyProperty上作为字段的值来公开只读依赖属性的public static readonly依赖属性标识符。
只读依赖属性是现有 API 和自定义方案中相当典型的方案,因为其他WPF功能可能需要依赖属性,即使该属性不是由调用方设置的。 可以使用只读依赖属性的值作为采用依赖属性的其他属性系统操作的基础,例如基于 Trigger 样式中的依赖属性。
有关依赖属性注册的详细信息,请参阅 DependencyProperty。
适用于
RegisterReadOnly(String, Type, Type, PropertyMetadata, ValidateValueCallback)
使用指定的属性类型、所有者类型、属性元数据和验证回调注册只读依赖属性。
public:
static System::Windows::DependencyPropertyKey ^ RegisterReadOnly(System::String ^ name, Type ^ propertyType, Type ^ ownerType, System::Windows::PropertyMetadata ^ typeMetadata, System::Windows::ValidateValueCallback ^ validateValueCallback);
public static System.Windows.DependencyPropertyKey RegisterReadOnly(string name, Type propertyType, Type ownerType, System.Windows.PropertyMetadata typeMetadata, System.Windows.ValidateValueCallback validateValueCallback);
static member RegisterReadOnly : string * Type * Type * System.Windows.PropertyMetadata * System.Windows.ValidateValueCallback -> System.Windows.DependencyPropertyKey
Public Shared Function RegisterReadOnly (name As String, propertyType As Type, ownerType As Type, typeMetadata As PropertyMetadata, validateValueCallback As ValidateValueCallback) As DependencyPropertyKey
参数
- name
- String
要注册的依赖属性的名称。
- propertyType
- Type
属性类型。
- ownerType
- Type
正在注册依赖属性的所有者类型。
- typeMetadata
- PropertyMetadata
依赖属性的属性元数据。
- validateValueCallback
- ValidateValueCallback
对用户创建的回调的引用,该回调应在典型类型验证之外执行依赖项属性值的任何自定义验证。
返回
一个依赖属性键,该键应用于在类中设置静态只读字段的值,然后用于稍后引用依赖属性。
注解
此方法返回类型 DependencyPropertyKey,而 RegisterAttached 返回类型 DependencyProperty。 通常,表示只读属性的键不是公开的,因为这些键可用于通过调用 SetValue(DependencyPropertyKey, Object)来设置依赖属性值。 类设计会影响你的要求,但通常建议将任何对象的 DependencyPropertyKey 访问和可见性限制为仅将依赖属性设置为类或应用程序逻辑的一部分所需的代码部分。 此外,建议通过公开类DependencyPropertyKey.DependencyProperty上作为字段的值来公开只读依赖属性的public static readonly依赖属性标识符。
只读依赖项属性是一个相当典型的方案。 可以使用只读依赖属性的值作为采用依赖属性的其他属性系统操作的基础,例如基于 Trigger 样式中的依赖属性。
有关依赖属性注册的详细信息,请参阅 DependencyProperty。
对只读依赖属性的验证可能不太重要。 为密钥指定的非公共访问级别可降低任意无效输入的可能性。