code_junkie

WPF: Binding source is string with path to property

Nov 12th, 2011
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. public partial class MyClass1 : Window
  2. {
  3. public MyClass2 MyClass2Object { get; set; }
  4.  
  5. public MyClass1()
  6. {
  7. InitializeComponent();
  8. MyClass2Object = new MyClass2();
  9. Binding binding = new Binding();
  10. binding.Source = MyClass2Object;
  11. binding.Path = new PropertyPath("StringVar");
  12. TextBoxFromXaml.SetBinding(TextBox.TextProperty, binding);
  13. }
  14. }
  15. public class MyClass2
  16. {
  17. public string StringVar { get; set; }
  18.  
  19. public MyClass2()
  20. {
  21. StringVar = "My String Here";
  22. }
  23. }
  24.  
  25. public partial class Window1 : Window
  26. {
  27. public MyClass2 MyClass2Object { get; set; }
  28. public Window1()
  29. {
  30. // use data context instead of source
  31. DataContext = this;
  32.  
  33. InitializeComponent();
  34.  
  35. MyClass2Object = new MyClass2();
  36. Binding binding = new Binding();
  37. binding.Path = new PropertyPath("MyClass2Object.StringVar");
  38. TextBoxFromXaml.SetBinding(TextBox.TextProperty, binding);
  39. }
  40. }
  41.  
  42. public class MyClass2
  43. {
  44. public string StringVar { get; set; }
  45. public MyClass2()
  46. {
  47. StringVar = "My String Here";
  48. }
  49. }
Add Comment
Please, Sign In to add comment