-5

私は今2日間頭をかきました。私は .NET を初めて使用しますが、20 を超える投稿と質問を読みました。私のコードは機能するはずです。いくつかは光を投げてください。

XAML:

<TextBox Grid.Column="3" Name="testgrid" Text="{Binding textsource, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>

コードビハインド:

public MainWindow()
{
    InitializeComponent();
    textbind tb = new textbind();
    tb.textsource = "one"; //one is displayed in the textbox.
    testgrid.DataContext = tb;
}

と:

public class textbind : INotifyPropertyChanged
 {
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(info));
          }
        }

    private string a=string.Empty;
    public textbind()
    {

    }


    public string textsource
    {
        get { return a; }
        set
        {
            if (value != a)
            {
                a = value;
                NotifyPropertyChanged(textsource);
            }
        }


         }
}

プロパティの変更:

public class changevalue
{
   //code doing things. this class is initialized by some other processes. 
     textbind tb1 = new textbind();
     tb1.textsource = "two"; // no updates to two in the text box.
}

私が信じていたのは、textsource プロパティを変更するたびに、テキストボックスの変更が反映されるということですが、それは起こりません。誰か助けてください。

ありがとう。

4

3 に答える 3

4
if (value != a)
{
    a = value;
    NotifyPropertyChanged("textsource");
}

textsource を変数として渡していて、NotifyPropertyChanged が textsource の実際の値を上げていました。代わりに、その名前「textsource」を渡す必要があります。

于 2013-06-21T09:51:27.560 に答える
2

バインドされたオブジェクトとはまったく異なるオブジェクトを編集しています。

あなたがメインウィンドウクラスにいる限り、あなたはできる

((textbind)testgrid.DataContext).textsource = "two";

メインウィンドウ クラスにいない場合は、datacontext に入れた textbind のインスタンスを、更新を行っているメソッドに確実に渡す必要があります。

さらに、textsource の実装を次のように変更する必要があります。

public string textsource
{
  get { return a; }
  set
  {
    if (value != a)
    {
      a = value;
      NotifyPropertyChanged("textsource");
    }
  }
}

変更されたプロパティの名前は、その値ではなく、notifypropertychanged に渡す必要があります。

Naresh による新しいコメントに応じて更新します。

インスタンスを追跡してコード内で渡す必要があるため、たとえば変更値クラスで次のようにします。

public class changevalue {
 public void doChange(textbind source) {
     source.textsource = "two"; // no updates to two in the text box.
 }
}

メインフォームから呼び出す場合は、textbind インスタンスを doChange 関数に渡す必要があります。

または、あなたができる

public class changevalue {
   public textbind source {get; private set;}

   public changevalue() {
      this.source = new textbind();
   }

   public void doChange() {
     source.textsource = "two"; // no updates to two in the text box.
   }
}

その後、changevalue クラスを初期化するたびに、フォームへの参照が必要になります。それからあなたはすることができます

var myChangeValue = new changevalue();
mymainform.DataContext = myChangeValue.source;
于 2013-06-21T10:04:07.867 に答える
2

ビューは、textbindオブジェクトの 1 つのインスタンス (この場合はtb) にバインドされます。そのオブジェクトを変更すると、ビューに反映されます。

tb.textsource = "new value";

しかし、あなたがしているのは、ビューにバインドされていない新しいtextbindオブジェクト ( tb1) を作成することであり、ビューが変更されたときにビューを変更することを期待しています。tb1それが機能するには、 newに設定する必要がありますDataContext。ただし、実際にすべきことはtextbind、最初にバインドされたオブジェクトのプロパティを変更することです。

また、@ Nightwish91 が述べたように、NotifyPropertyChanged呼び出しは次のようになります。

NotifyPropertyChanged("textsource");

そうしないと、ビューは誤ったプロパティ変更通知を受け取り、期待どおりに更新されません。

于 2013-06-21T09:54:19.190 に答える