Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将控件属性绑定到Window ViewModel类的属性_C#_Wpf_Binding - Fatal编程技术网

C# 将控件属性绑定到Window ViewModel类的属性

C# 将控件属性绑定到Window ViewModel类的属性,c#,wpf,binding,C#,Wpf,Binding,我想将TextBox的Text属性绑定到ViewModel属性的子级 这是我的密码: foo.cs: public class foo() { public foo() { Bar = "Hello World"; } public string Bar { Get; private Set;} //Some functions } ViewModel.cs: public clas

我想将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>

我也试过了
而且,它们都不适合我。

您已经在
视图模型上实现了
INotifyPropertyChanged
,但在
属性更改时您从未调用它

尝试:

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

  .................