0

TextBlockモデルのプロパティにバインドするものがあります。ウィンドウにバインドされているビューモデルに座っているモデル。

<TextBlock Text="{Binding MyModel.TextVar,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBlock>

TextVarは、Notifyの関数を呼び出す文字列プロパティです。

だから、なぜそれが機能しないのか分かりません。(OutPutにはバインディングエラーはありません)。

編集:

    string _textVar;
    public string TextVar
    {
        get
        {
            return _textVar;
        }
        set
        {
            _textVar= value;
            NotifyPropertyChanged("TextVar");
        }
    }


    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)-- HERE THE PROPBLEM, IT ARRIVE NULL
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
4

2 に答える 2

0

Mode=TwoWay で TextBlock をバインドしても意味がありません。これは、TextBox ではなく TextBlock であるためです。

それでも、テキストブロックの DataContext にプロパティ "MyModel" があり、この "MyModel" - オブジェクトにプロパティ "TextVar" がある場合、バインディングは機能します。

実行時にデータコンテキストとバインディングを調べたい場合は、Snoopを使用します

編集:あなたがしたようにバインディングを行う場合に必要なもの

public class MyViewmodel
{
  public MyOtherClass MyModel {get;set;}
}

public class MyOtherClass
{
   public string TextVar {get;set;}
}

windows.xaml.cs ctor:

 this.data = new MyViewmodel(); 
 this.DataContext = this.data;
于 2013-02-19T07:08:30.690 に答える