2012-03-26 3 views
7

これは単純なはずですが、動作させることはできません。 私はタイプのproperyを定義したウィンドウ(メインXAMLアプリのウィンドウ)TextBlockをウィンドウのプロパティにバインドする

を持っている「テスト」(持ち、IDと日時TestDateをint型)

 public Test CurrentTest 
    { 
     get 
     { 
      return currentTest; 
     } 
     set 
     { 
      currentTest = value; 
      OnPropertyChanged("CurrentTest"); 
     } 
    } 

私は、OnPropertyChangedをするImplを追加しました:

public event PropertyChangedEventHandler PropertyChanged; 
    private void OnPropertyChanged(String property) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(property)); 
     } 
    } 

これで、ウィンドウのテキストブロックにバインドしようとしました。 しかし、それは動作しません:

<TextBlock Text="{Binding Source={StaticResource CurrentTest}, Path=TestDate, StringFormat=dd/MM/yyyy, TargetNullValue=Not Yet Set}"></TextBlock> 

、これはどちらか動作しません:

<TextBlock> 
      <TextBlock.Text> 
       <Binding ElementName="CurrentTest" Path="TestDate" TargetNullValue="not yet set" Mode="OneWay"></Binding> 
      </TextBlock.Text> 
     </TextBlock> 

私はTextBlockには、このプロパティの日付を表示持つように何をすべきか?

+2

しかし私はのPropertyChangedコードの実装の横にあなたのクラスの後ろにこれを持っているか、あなたはDataContextの権利を設定する方法を知っていると思いますか? :INotifyPropertyChanged – Silvermind

+0

また、特定のバインディングエラーについてoutputwindowをチェックしましたか? – Silvermind

+0

あなたはどこにいるのでしょうかINotifyPropertyChanged – Dani

答えて

19

あなたはRelativeSourceプロパティを使用することができます。

<TextBlock Text="{Binding Path=CurrentTest.TestDate, 
          RelativeSource={RelativeSource Mode=FindAncestor, 
                 AncestorType=Window}}" /> 
+0

これはうまくいきますが、一度しか機能しませんでした.WindowクラスにINotifyPropertyChangedを追加しました。ありがとうございます@SilverMindも! – Dani

+0

なぜStaticResourceが機能しないのですか? – Dani

+0

@ダニ、なぜそれはすべきだと思いますか? StaticResourceは、ビューのプロパティではなく、リソースにアクセスするために使用されます。 –

関連する問題