code_junkie

Two way data binding in a GridView

Nov 14th, 2011
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. Title="Window1" Height="300" Width="300">
  2. <Grid>
  3. <ListView Name="TestList">
  4. <ListView.View>
  5. <GridView>
  6. <GridViewColumn Header="Strings">
  7. <GridViewColumn.CellTemplate>
  8. <DataTemplate>
  9. <TextBox Text="{Binding Path=String, Mode=TwoWay}"/>
  10. </DataTemplate>
  11. </GridViewColumn.CellTemplate>
  12. </GridViewColumn>
  13. <GridViewColumn Header="Bools">
  14. <GridViewColumn.CellTemplate>
  15. <DataTemplate>
  16. <CheckBox IsChecked="{Binding Path=Bool, Mode=TwoWay}"/>
  17. </DataTemplate>
  18. </GridViewColumn.CellTemplate>
  19. </GridViewColumn>
  20. </GridView>
  21. </ListView.View>
  22. </ListView>
  23. </Grid>
  24. </Window>
  25.  
  26. using System.Collections.Generic;
  27. using System.Windows;
  28.  
  29. namespace GridViewTextbox
  30. {
  31. public partial class Window1 : Window
  32. {
  33. private List<TestRow> _rows = new List<TestRow>();
  34.  
  35. public Window1()
  36. {
  37. InitializeComponent();
  38.  
  39. _rows.Add(new TestRow("a", false));
  40. _rows.Add(new TestRow("b", true));
  41. _rows.Add(new TestRow("c", false));
  42.  
  43. TestList.ItemsSource = _rows;
  44. TestList.DataContext = _rows;
  45. }
  46. }
  47.  
  48. public class TestRow : System.Windows.DependencyObject
  49. {
  50. public TestRow(string s, bool b)
  51. {
  52. String = s;
  53. Bool = b;
  54. }
  55.  
  56. public string String
  57. {
  58. get { return (string)GetValue(StringProperty); }
  59. set { SetValue(StringProperty, value); }
  60. }
  61.  
  62. // Using a DependencyProperty as the backing store for String. This enables animation, styling, binding, etc...
  63. public static readonly DependencyProperty StringProperty =
  64. DependencyProperty.Register("String", typeof(string), typeof(TestRow), new UIPropertyMetadata(""));
  65.  
  66.  
  67. public bool Bool
  68. {
  69. get { return (bool)GetValue(BoolProperty); }
  70. set { SetValue(BoolProperty, value); }
  71. }
  72.  
  73. // Using a DependencyProperty as the backing store for Bool. This enables animation, styling, binding, etc...
  74. public static readonly DependencyProperty BoolProperty =
  75. DependencyProperty.Register("Bool", typeof(bool), typeof(TestRow), new UIPropertyMetadata(false));
  76. }
  77. }
  78.  
  79. public static readonly DependencyProperty BoolProperty =
  80. DependencyProperty.Register("Bool", typeof(bool), typeof(TestRow), new UIPropertyMetadata(false, OnBoolChanged));
  81. private static void OnBoolChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
  82. {
  83. //this method will be called everytime Bool changes value
  84. }
  85.  
  86. yourControl.SetValue(TestRow.StringProperty, someValue);
  87.  
  88. yourControl.String = someValue;
Add Comment
Please, Sign In to add comment