programing tip

읽기 전용 종속성 속성을 어떻게 생성합니까?

itbloger 2020. 11. 27. 07:52
반응형

읽기 전용 종속성 속성을 어떻게 생성합니까?


읽기 전용 종속성 속성을 어떻게 생성합니까? 그렇게하기위한 모범 사례는 무엇입니까?

특히 저를 가장 어렵게 만드는 것은 구현이 없다는 사실입니다.

DependencyObject.GetValue()  

즉 소요 System.Windows.DependencyPropertyKey파라미터로.

System.Windows.DependencyProperty.RegisterReadOnlyependencyPropertyKey대신 D 객체를 반환합니다 DependencyProperty. 그렇다면 GetValue를 호출 할 수없는 경우 읽기 전용 종속성 속성에 어떻게 액세스해야합니까? 아니면 어떻게 든 DependencyPropertyKey평범한 오래된 DependencyProperty물건 으로 변환해야 합니까?

조언 및 / 또는 코드는 대단히 감사하겠습니다!


실제로 ( RegisterReadOnly 를 통해 ) 쉽습니다 .

public class OwnerClass : DependencyObject // or DependencyObject inheritor
{
    private static readonly DependencyPropertyKey ReadOnlyPropPropertyKey
        = DependencyProperty.RegisterReadOnly("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세터 로 인해 이것은 투명합니다.

참고 URL : https://stackoverflow.com/questions/1122595/how-do-you-create-a-read-only-dependency-property

반응형