我想将 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();
}
窗口.xaml:
<!--
Some controls
-->
<TextBox Text="{Binding Path=Property.Bar}"></TextBox>