3

TextBox の Text プロパティを ViewModel のプロパティの子にバインドしたいと考えています。

これが私のコードです:


foo.cs:

    public class foo()
    {
      public foo()
      {
        Bar = "Hello World";
      }

      public string Bar { Get; private Set;}
      //Some functions
    }

ViewModel.cs:

    public class ViewModel : INotifyPropertyChanged
    {
      public foo Property { Get; Set; }

      //some more properties

      public ViewModel()
      {

      }

      public event PropertyChangedEventHandler PropertyChanged;        
      protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));            
      }
    }

Window.xaml.cs:

    public ViewModel MyViewModel { get; set; }

    public Window()
    {
      MyViewModel = new ViewModel();
      this.DataContext = MyViewModel;
      MyViewModel.Property = new foo();
    }

Window.xaml:

    <!--
    Some controls
    -->

    <TextBox Text="{Binding Path=Property.Bar}"></TextBox>

thisthisも試し ましたが、どれもうまくいきませんでした。

4

1 に答える 1

4

あなたはあなたに実装INotifyPropertyChangedしましたが、変更されViewModelたときにそれを呼び出すことはありませんProperty

試す:

public class ViewModel : INotifyPropertyChanged
{
  private foo _property;
  public foo Property
  { 
     get{ return _property; }
     set{ _property = value; OnPropertyChanged(); }
  }

  .................
于 2013-08-06T10:25:20.860 に答える