Posted By

qrist0ph on 09/25/08


Tagged

WPF dataBinding


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

umang_nine
khouser


DataBinding with procedural Code


 / Published in: C#
 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14.  
  15. namespace WPF_Testground
  16. {
  17. /// <summary>
  18. /// Interaction logic for Window1.xaml
  19. /// </summary>
  20. public partial class Window1 : Window
  21. {
  22. BindingSource bindingSource = new BindingSource();
  23. public Window1()
  24. {
  25. InitializeComponent();
  26. createTextBlock();
  27. }
  28.  
  29. public void createTextBlock()
  30. {
  31.  
  32. BindingSource bindingSource2 = new BindingSource();
  33.  
  34. TextBlock tb = new TextBlock();
  35. tb.Text = "Eine Box";
  36. Binding binding = new Binding();
  37. binding.Source = bindingSource;
  38. binding.Path = new PropertyPath("Wert");
  39. tb.SetBinding(TextBlock.TextProperty, binding);
  40. grid.Children.Add(tb);
  41. }
  42.  
  43. private void button1_Click(object sender, RoutedEventArgs e)
  44. {
  45. bindingSource.Wert++;
  46. }
  47.  
  48. }
  49.  
  50. class BindingSource : System.Windows.DependencyObject
  51. {
  52. public static System.Windows.DependencyProperty WertProperty;
  53.  
  54. public BindingSource()
  55. {
  56. if (BindingSource.WertProperty == null)
  57. {
  58. BindingSource.WertProperty = System.Windows.DependencyProperty.Register("Wert", typeof(int), typeof(BindingSource));
  59. }
  60. SetValue(BindingSource.WertProperty, 0);
  61. }
  62.  
  63. public int Wert
  64. {
  65. get { return Int32.Parse(GetValue(BindingSource.WertProperty).ToString()); }
  66. set { SetValue(BindingSource.WertProperty, value); }
  67. }
  68. }
  69. }

Report this snippet   <

You need to login to post a comment.