2009-07-13 32 views
52

Salt okunur dependancy özelliği nasıl oluşturulur? Bunu yapmak için en iyi uygulamalar nelerdir?Salt Okunur Bir Bağımlılık Mülkiyeti Nasıl Oluşturulur?

Özellikle, ne parametre olarak bir System.Windows.DependencyPropertyKey alır

DependencyObject.GetValue() 

hiçbir uygulama yok olması beni en edilir stumping ediyor.

System.Windows.DependencyProperty.RegisterReadOnly, DependencyProperty yerine bir D ependencyPropertyKey nesnesini döndürür. Peki, GetValue'a herhangi bir çağrı yapamazsanız, salt okunur bağımlılık özelliğine nasıl erişmeniz gerekiyor? Yoksa DependencyPropertyKey'u eski bir DependencyProperty nesnesine dönüştürmeniz gerekiyor mu?

Tavsiye ve/veya kod, GREATLY takdir edilecektir!

cevap

113

Aslında (RegisterReadOnly yoluyla), kolay: Eğer Özel/korumalı/dahili kod değerini ayarladığınızda

public class OwnerClass : DependencyObject // or DependencyObject inheritor 
{ 
    private static readonly DependencyPropertyKey ReadOnlyPropPropertyKey 
     = DependencyProperty.RegisterReadOnly("ReadOnlyProp", typeof(int), typeof(OwnerClass), 
      new FrameworkPropertyMetadata((int)0, 
       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 ... 
} 

Yalnızca tuşunu kullanın. Korunan ReadOnlyProp ayarlayıcısından dolayı, bu size şeffaftır.

+0

Tamamlanmış bir kod değil. – Developer

+1

@Singh Nasıl "tamamlanmadı" demek istiyorsun? –

+0

Genel Sınıf Sahibi Sınıfı'ndan bahsetmiyorsunuz. Eğer yeni bir geliştirici bu kodu görürse, o zaman OwnerClass'ın ne güncellediği hakkında kafasını karıştırır. – Developer

İlgili konular