2012-05-09 10 views
6

依存プロパティを学習しています。私は多くの投稿&本を読むが、まだ私ははっきりしていない。依存プロパティエラー

下記のプログラムは、私が覚えて書いたものです。その中に何らかのエラーがありましたら、解決にお役立てください。私は質問があります。

  1. カスタム依存プロパティエレメントの主な用途は、変更の通知用ですか?
  2. の「IsDefaultProperty」コードが見つかりました。 WPFテキストブックにあります。これは、 'IsDefault'プロパティが依存関係プロパティであることを意味しますか?
  3. なぜ彼らはそのコードを示しましたか?これは、内部的にButtonクラスで定義されていることを意味しますか? (内部コードを示していますか?)またはカスタムとして定義する方法を示しましたか?ここで

私のコードは次のとおりです。

namespace DependencyProperties 
{ 
    public class Contact 
    { 
     private int id=100; 
     private string name="shri"; 
     public static readonly DependencyProperty IsPresentProperty; 

     public int ID 
     { 
      get { return id; } 
     } 
     public string NAME 
     { 
      get { return name; } 
     } 

     static Contact() 
     { 
      IsPresentProperty = DependencyProperty.Register("IsPresent", typeof(bool),typeof(Contact),new FrameworkPropertyMetadata(false,new PropertyChangedCallback(OnIsPresentChanged))); 
     } 

     public bool Present 
     { 
      get { return (bool)GetValue(Contact.IsPresentProperty); } 
      set { SetValue(Contact.IsPresentProperty, value); } 
     } 

     private static void OnIsPresentChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) 
     { 

     } 
    } 
} 

そして私は、エラーを参照してください。また、クラスは、同様INotifyPropertyChangedを実装することによって配置することができ

> Error: GetValue and SetValue does not exist in the current context 

答えて

14

The main use of custom Dependency property element is for the notification of change?

ありませんが、。依存関係のプロパティは変更通知を提供しますが、実際の根拠は異なるものです。 Why dependency properties?How is the WPF property system economical?

I found an 'IsDefaultProperty' code for Button in a WPF text book. It means 'IsDefault' property is a dependency property?

はい]を参照してください。フィールド "FooBarProperty"の命名は、依存関係プロパティを定義するためのWPF規約です。 IsDefaultPropertyのドキュメントを確認して、それが実際に依存プロパティであることを確認し、さらにIsDefaultドキュメントに「依存プロパティ情報」というセクションがあることを確認することができます。私は

Why they shown that code? It means, internally, in Button class, its defined like that? (They showed internal code?) or they showed how to define as custom?

「はコードがある 『と』が、はい、プロパティはほぼ確実Buttonにそのように定義されているかわからないM(私はので 『ほとんど』」あなたが参照しているか分かりません)。

Error: GetValue and SetValue does not exist in the current context

これは、あなたがDependencyObjectから派生していないためです。

12

DependencyPropertyインスタンスはDependencyObjectのインスタンスで定義する必要があります。したがって、クラスはDependencyObject、またはそのサブクラスの1つから派生する必要があります。 Buttonなど、WPFの多くの種類がこれから派生しています。

だからあなたのコードは、この場合での作業を取得するために、あなたが使用する必要があります。

public class Contact : DependencyObject 
{ 
    // ... 

あなたはGetValueSetValue上のエラーを受けている理由です - 彼らはDependencyObject上で定義されています。

+0

ありがとうございます。エラーが解決しました。 – SHRI

+0

非常に役に立ちます。この回答には、より多くの票が必要です。 –