programing

읽기 전용 종속성 속성을 생성하려면 어떻게 해야 합니까?

lastcode 2023. 4. 17. 21:54
반응형

읽기 전용 종속성 속성을 생성하려면 어떻게 해야 합니까?

읽기 전용 종속성 속성은 어떻게 생성합니까?이를 위한 베스트 프랙티스는 무엇입니까?

특히, 저를 가장 당황하게 하는 것은, 이 모든 것들이 제대로 구현되지 않았다는 사실입니다.

DependencyObject.GetValue()  

그 일에는System.Windows.DependencyPropertyKey파라미터로 지정합니다.

System.Windows.DependencyProperty.RegisterReadOnlyD를 반환하다ependencyPropertyKey보다 반대하다DependencyProperty그럼 GetValue에 콜을 할 수 없는 경우 읽기 전용 의존관계 속성에 어떻게 액세스해야 합니까?아니면 어떻게 해서든 이 모든 걸 바꿔서DependencyPropertyKey평범한 노인으로.DependencyProperty오브젝트?

조언 및/또는 코드를 알려주시면 감사하겠습니다!

(Register Read Only를 통해) 실제로 간단합니다.

public class OwnerClass : DependencyObject // or DependencyObject inheritor
{
    private static readonly DependencyPropertyKey ReadOnlyPropPropertyKey
        = DependencyProperty.RegisterReadOnly(
            nameof(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세터, 이건 당신에게 투명해요.

현시점에서는 https://github.com/HavenDV/DependencyPropertyGenerator,을 사용하는 것이 좋습니다.코드는 매우 간단합니다.

[DependencyProperty<int>("ReadOnlyProperty", IsReadOnly = true)]
public partial class MyControl : UserControl
{
}

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

반응형