Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

I want to Bind the Text Property of a TextBox to a Child of a of a Property of a ViewModel.

Here is my code:


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> 

I also tried thisthis and thisthis, but none of them worked for me.

I want to Bind the Text Property of a TextBox to a Child of a of a Property of a ViewModel.

Here is my code:


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> 

I also tried this and this, but none of them worked for me.

I want to Bind the Text Property of a TextBox to a Child of a of a Property of a ViewModel.

Here is my code:


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> 

I also tried this and this, but none of them worked for me.

edited title
Link
brunnerh
  • 188.1k
  • 30
  • 362
  • 434

C# WPF Bind an control property to a property of a Class of the Window ViewModel

Source Link
Wr4thon
  • 554
  • 1
  • 5
  • 11

C# WPF Bind an control property to a property of a Class of the Window ViewModel

I want to Bind the Text Property of a TextBox to a Child of a of a Property of a ViewModel.

Here is my code:


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> 

I also tried this and this, but none of them worked for me.