종속성 속성에 대한 메타데이터를 재정의하는 방법(WPF .NET)

종속성 속성을 정의하는 클래스에서 파생되는 경우 종속성 속성과 해당 메타데이터를 상속합니다. 이 문서에서는 OverrideMetadata 메서드를 호출하여 상속된 종속성 속성의 메타데이터를 재정의하는 방법을 설명합니다. 메타데이터를 재정의하면 상속된 종속성 속성의 특성을 하위 클래스별 요구 사항과 일치하도록 수정할 수 있습니다.

중요

.NET 7 및 .NET 6에 관한 데스크톱 가이드 설명서는 제작 중입니다.

배경

종속성 속성을 정의하는 클래스는 PropertyMetadata 또는 FrameworkPropertyMetadata와 같은 파생 형식 중 하나에서 특성을 지정할 수 있습니다. 이러한 특성 중 하나는 종속성 속성의 기본값입니다. 종속성 속성을 정의하는 많은 클래스는 종속성 속성 등록 중에 속성 메타데이터를 지정합니다. 등록 중에 메타데이터가 지정되지 않은 경우 WPF 속성 시스템은 기본값을 사용하여 PropertyMetadata 개체를 할당합니다. 클래스 상속을 통해 종속성 속성을 상속하는 파생 클래스에는 종속성 속성의 원래 메타데이터를 재정의하는 옵션이 있습니다. 이러한 방식으로 파생 클래스는 클래스 요구 사항을 충족하도록 종속성 속성 특성을 선택적으로 수정할 수 있습니다. OverrideMetadata(Type, PropertyMetadata)를 호출할 때 파생 클래스는 고유한 형식을 첫 번째 매개 변수로 지정하고 메타데이터 인스턴스를 두 번째 매개 변수로 지정합니다.

종속성 속성에 대한 메타데이터를 재정의하는 파생 클래스는 속성 시스템에서 속성을 사용하기 전에 이 작업을 수행해야 합니다. 종속성 속성은 속성을 등록하는 클래스의 인스턴스가 인스턴스화될 때 사용합니다. 이 요구 사항을 충족하려면 파생 클래스가 정적 생성자 내에서 OverrideMetadata를 호출해야 합니다. 소유자 형식이 인스턴스화된 후 종속성 속성의 메타데이터를 재정의하면 예외가 발생하지 않지만 속성 시스템에서 일관되지 않은 동작이 발생합니다. 또한 파생 형식은 종속성 속성의 메타데이터를 두 번 이상 재정의할 수 없으며, 이렇게 하려고 하면 예외가 발생합니다.

예제

다음 예제에서 파생 클래스 TropicalAquarium은 기본 클래스 Aquarium에서 상속된 종속성 속성의 메타데이터를 재정의합니다. 메타데이터 형식은 AffectsRender와 같은 UI 관련 WPF 프레임워크 특성을 지원하는 FrameworkPropertyMetadata입니다. 파생 클래스는 상속된 AffectsRender 플래그를 재정의하지 않지만 파생 클래스 인스턴스에서 기본값 AquariumGraphic을 업데이트합니다.

public class Aquarium : DependencyObject
{
    // Register a dependency property with the specified property name,
    // property type, owner type, and property metadata.
    public static readonly DependencyProperty AquariumGraphicProperty =
        DependencyProperty.Register(
          name: "AquariumGraphic",
          propertyType: typeof(Uri),
          ownerType: typeof(Aquarium),
          typeMetadata: new FrameworkPropertyMetadata(
              defaultValue: new Uri("http://www.contoso.com/aquarium-graphic.jpg"),
              flags: FrameworkPropertyMetadataOptions.AffectsRender)
        );

    // Declare a read-write CLR wrapper with get/set accessors.
    public Uri AquariumGraphic
    {
        get => (Uri)GetValue(AquariumGraphicProperty);
        set => SetValue(AquariumGraphicProperty, value);
    }
}
Public Class Aquarium
    Inherits DependencyObject

    ' Register a dependency property with the specified property name,
    ' property type, owner type, and property metadata.
    Public Shared ReadOnly AquariumGraphicProperty As DependencyProperty =
        DependencyProperty.Register(
            name:="AquariumGraphic",
            propertyType:=GetType(Uri),
            ownerType:=GetType(Aquarium),
            typeMetadata:=New FrameworkPropertyMetadata(
                defaultValue:=New Uri("http://www.contoso.com/aquarium-graphic.jpg"),
                flags:=FrameworkPropertyMetadataOptions.AffectsRender))

    ' Declare a read-write CLR wrapper with get/set accessors.
    Public Property AquariumGraphic As Uri
        Get
            Return CType(GetValue(AquariumGraphicProperty), Uri)
        End Get
        Set
            SetValue(AquariumGraphicProperty, Value)
        End Set
    End Property

End Class
public class TropicalAquarium : Aquarium
{
    // Static constructor.
    static TropicalAquarium()
    {
        // Create a new metadata instance with a modified default value.
        FrameworkPropertyMetadata newPropertyMetadata = new(
            defaultValue: new Uri("http://www.contoso.com/tropical-aquarium-graphic.jpg"));

        // Call OverrideMetadata on the dependency property identifier.
        // Pass in the type for which the new metadata will be applied
        // and the new metadata instance.
        AquariumGraphicProperty.OverrideMetadata(
            forType: typeof(TropicalAquarium),
            typeMetadata: newPropertyMetadata);
    }
}
Public Class TropicalAquarium
    Inherits Aquarium

    ' Static constructor.
    Shared Sub New()
        ' Create a new metadata instance with a modified default value.
        Dim newPropertyMetadata As New FrameworkPropertyMetadata(
            defaultValue:=New Uri("http://www.contoso.com/tropical-aquarium-graphic.jpg"))

        ' Call OverrideMetadata on the dependency property identifier.
        ' Pass in the type for which the new metadata will be applied
        ' and the new metadata instance.
        AquariumGraphicProperty.OverrideMetadata(
            forType:=GetType(TropicalAquarium),
            typeMetadata:=newPropertyMetadata)
    End Sub

End Class

참고 항목