2009-04-07 12 views
1

私は苦労しているデータバインディングに関する質問があります。WPFデータの文字列へのバインド

I持って私のxaml.csファイルで次のプロパティ:

private string _stationIdInstruction; 

    public event PropertyChangedEventHandler PropertyChanged; 

    public string StationIdInstruction 
    { 
     get { return _stationIdInstruction; } 
     set 
     { 
      _stationIdInstruction = value; 
      OnPropertyChanged("StationIdInstruction"); 
     } 
    } 

    protected void OnPropertyChanged(string name) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(name)); 
     } 
    } 

それはそのテキストとして文字列プロパティをピックアップし、私が更新するときTextBlock.Textを更新して、私はStationIdInstructionsへのTextBlockをバインドするにはどうすればよいですStationIdInstructions。

何か助けていただければ幸いです。

+0

ソースコードに小さなエラーがあります。オープン「{」(「if(PropertyChanged!= null)」の後に忘れたか、存在しないif-blockを閉じるかのいずれかです)。 – vstrien

答えて

4

をはいて、バインディングコンテキストを指定することを忘れないでください。たとえば、

<Window ... Name="MyWindow"> 
    <Grid DataContext="{Binding ElementName=MyWindow, Path=.}"> 
    <TextBlock Text="{Binding Path=StationIdInstruction}" /> 
-1

はそうのようなあなたのStationIdInstructionsがあなたのコントロールのDataContextの上でオブジェクトを設定して、あなたのTextBlock:

<TextBlock Text="{Binding StationIdInstruction}" /> 
+0

動作しません。エラーは、依存関係のプロパティが存在しないことを示します。 – Krythic

関連する問題