7-10 2008

Using DataContext to Simplify Databinding on Multiple Elements in WPF

I was recently working on a WPF form that allowed users to edit data stored in an XML file.  In the process, I learned a lot about simplifying my databinding code.  As a WPF newbie myself, I found the use of datacontexts on parent elements just pretty cool.  You can bind whole blocks of elements to the same source, and they are all updated together with almost no code-behind whatsoever.

For example, consider the following block of xml to be loaded into a WPF form

<files> <localfiles type="pictures" description="local picture files"> <file name="picture1.jpg" folder="pictures/samplepictures" title="autumn leaves" size="304k" /> <file name="picture2.jpg" folder="pictures/july2008" title="butterfly" size="139k" /> <file name="picture3.jpg" folder="pictures/june2008" title="desert" size="269k" /> <file name="picture4.jpg" folder="pictures/july2008" title="clouds" size="135k" /> </localfiles> </files>

First, I bind my textblocks to attributes of the xml elements, and wrap them in a parent element, like a stackpanel.

<StackPanel Margin="15" x:Name="spFiles"> 
<TextBlock Foreground="#FF000000">Type</TextBlock> <TextBox x:Name="tbType" Width="220" Height="25" Text="{Binding Path=Attribute[type].Value}"/> <TextBlock Foreground="#FF000000">Description</TextBlock> <TextBox x:Name="tbDescription" Width="220" Height="25" Text="{Binding Path=Attribute[description].Value}"/> <TextBlock Foreground="#FF000000">Files</TextBlock>

Then I can programmatically set the datacontext of that parent element when the user opens the form. Here, "root" is a System.XML.Linq.XElement containing the root element of my xml document.

spFiles.DataContext = root.Element("localfiles");

If I want to use a selector, like a combobox, to select the file to be displayed in the form fields, I can bind the combobox to the elements of type "file" inside the datacontext.  Here's where I can also use the itemtemplate to bind the combobox items to a particular attribute of the elements themselves.  In this case, the combobox will show the names of the files.

<ComboBox x:Name="cbFiles" HorizontalAlignment="Center" Width="220" Height="25" SelectedIndex="0" ItemsSource="{Binding Path=Elements[file]}" IsSynchronizedWithCurrentItem="True"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=Attribute[name].Value}"/> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>

Finally, to use the selected item of the combobox to fill the rest of the form, I wrap those controls in a stackpanel, and bind its datacontext accordingly.  Even though they are still inside the spFiles panel, they will still bind to the most immediate parent's datacontext first; in this case the spFileInfo panel.

<StackPanel x:Name="spFileInfo" DataContext="{Binding ElementName=cbFiles, Path=SelectedItem}"> <TextBlock Foreground="#FF000000">Folder</TextBlock> <TextBox x:Name="tbFileFolder" Width="220" Height="25" Text="{Binding Path=Attribute[folder].Value}"/> <TextBlock Foreground="#FF000000">Title</TextBlock> <TextBox x:Name="tbTitle" Width="220" Height="25" Text="{Binding Path=Attribute[title].Value}"/> <TextBlock Foreground="#FF000000">Size</TextBlock> <TextBox x:Name="tbSize" Width="220" Height="25" Text="{Binding Path=Attribute[size].Value}"/> </StackPanel> </StackPanel>

Now my form will be loaded with the data, my combobox will be populated, and my folder, title, and size fields will be updated automatically when a combobox item is selected, all with basically one line of C# code!

image  image  image

Of course, since the default UpdateSourceTrigger mode of a textbox is LostFocus, any changes made will be automatically saved to the XElement root after they are entered, but I'll need to call root.Save in order to update the actual file.  I could also control the updates myself by setting the mode to Explicit in my binding XAML, and then calling UpdateSource from my code.  For more information on UpdateSourceTrigger, and on databinding in general, here's a great post on the MSDN blogs.

86 Comments

  1. 1 Anonymous informant 04 Apr
    The tag cloud advertisement makes this document useless.
  2. 2 email@gmail.com 08 Apr
    ztjlYQ Very good blog.Thanks Again. Fantastic.
  3. 3 golojan@gmail.com 10 Apr
    Awesome blog article.Thanks Again.
  4. 4 howardlee@gmail.com 10 Apr
    Really enjoyed this blog.Really looking forward to read more. Awesome.
  5. 5 henryfrod@gmail.com 10 Apr
    A round of applause for your blog.Really thank you! Much obliged.
  6. 6 greg.ham@gmail.com 10 Apr
    Wow, great blog post.Much thanks again. Much obliged.
  7. 7 mitea@gmail.com 10 Apr
    I value the blog.Thanks Again. Want more.
  8. 8 jymmijons@gmail.com 10 Apr
    Major thankies for the article.Really thank you! Will read on...
  9. 9 emai1l@gmail.com 11 Apr
    Im grateful for the article.Thanks Again. Cool.
  10. 10 email@gmail.com 11 Apr
    wow, awesome blog.Really looking forward to read more. Fantastic.
  11. 11 email@gmail.com 11 Apr
    Fantastic blog article.Thanks Again. Want more.
  12. 12 emai1l@gmail.com 11 Apr
    Muchos Gracias for your post.Much thanks again. Much obliged.
  13. 13 email@gmail.com 11 Apr
    Major thankies for the article post.Really thank you! Great.
  14. 14 susanzz@gmail.com 11 Apr
    Im grateful for the article.Really looking forward to read more. Keep writing.
  15. 15 email@gmail.com 11 Apr
    Thank you ever so for you article. Fantastic.
  16. 16 golojan@gmail.com 11 Apr
    I cannot thank you enough for the article.Really looking forward to read more. Cool.
  17. 17 email@gmail.com 11 Apr
    I really enjoy the blog post.Really thank you! Cool.
  18. 18 golojan@gmail.com 11 Apr
    Great, thanks for sharing this post.Much thanks again. Fantastic.
  19. 19 email@gmail.com 11 Apr
    I really enjoy the blog article.Really looking forward to read more. Want more.
  20. 20 huiztheman@gmail.com 11 Apr
    Thank you for your blog post.Much thanks again.
  21. 21 email@gmail.com 11 Apr
    Very informative post.Really looking forward to read more.
  22. 22 greg.ham@gmail.com 11 Apr
    I really liked your blog.Really looking forward to read more. Great.
  23. 23 email@gmail.com 11 Apr
    Say, you got a nice article post.Thanks Again. Cool.
  24. 24 howardlee@gmail.com 11 Apr
    Thank you ever so for you blog post. Awesome.
  25. 25 email@gmail.com 11 Apr
    I appreciate you sharing this post.Really thank you! Really Cool.
  26. 26 greg.ham@gmail.com 11 Apr
    Thank you ever so for you article post.Much thanks again. Keep writing.
  27. 27 email@gmail.com 11 Apr
    Im obliged for the blog post. Great.
  28. 28 huiztheman@gmail.com 11 Apr
    Im grateful for the article.Really thank you! Want more.
  29. 29 email@gmail.com 11 Apr
    Major thankies for the blog article.Really thank you! Much obliged.
  30. 30 howardlee@gmail.com 11 Apr
    Looking forward to reading more. Great blog post.Really looking forward to read more. Really Cool.
  31. 31 email@gmail.com 11 Apr
    I cannot thank you enough for the post. Will read on...
  32. 32 henryfrod@gmail.com 11 Apr
    Thanks for sharing, this is a fantastic blog article.Thanks Again. Great.
  33. 33 reti a doghe ortopediche anatomiche 11 Apr
    Say, you got a nice post.Thanks Again. Really Cool.
  34. 34 email@gmail.com 11 Apr
    I really enjoy the blog.Much thanks again. Really Cool.
  35. 35 huiztheman@gmail.com 11 Apr
    wow, awesome post.Really thank you! Want more.
  36. 36 email@gmail.com 11 Apr
    Awesome post.Much thanks again. Want more.
  37. 37 henryfrod@gmail.com 11 Apr
    Very good post.Really looking forward to read more. Really Great.
  38. 38 emai1l@gmail.com 12 Apr
    Appreciate you sharing, great blog post.Really thank you! Much obliged.
  39. 39 howardlee@gmail.com 12 Apr
    Thanks a lot for the article post.Thanks Again. Much obliged.
  40. 40 email@gmail.com 12 Apr
    Awesome blog article.Really looking forward to read more. Much obliged.
  41. 41 golojan@gmail.com 12 Apr
    I loved your blog post.Thanks Again. Fantastic.
  42. 42 best car stereo 12 Apr
    Great, thanks for sharing this blog.Really thank you! Much obliged.
  43. 43 email@gmail.com 12 Apr
    Im grateful for the blog article.Thanks Again. Awesome.
  44. 44 personalised photo frames 12 Apr
    Thank you for your blog article.Really thank you! Will read on...
  45. 45 email@gmail.com 12 Apr
    Appreciate you sharing, great article post.Thanks Again. Awesome.
  46. 46 howardlee@gmail.com 12 Apr
    Really appreciate you sharing this post.Really thank you! Keep writing.
  47. 47 free webcam girls 12 Apr
    Really informative post. Awesome.
  48. 48 email@gmail.com 12 Apr
    I really enjoy the blog post.Thanks Again. Great.
  49. 49 jymmijons@gmail.com 12 Apr
    I really liked your blog post.Much thanks again. Fantastic.
  50. 50 all one word men4rent 12 Apr
    Im grateful for the blog post.Thanks Again. Keep writing.
  51. 51 email@gmail.com 12 Apr
    Awesome blog article.Thanks Again. Really Cool.
  52. 52 huiztheman@gmail.com 12 Apr
    Thanks again for the blog.Much thanks again. Keep writing.
  53. 53 Polish Hearts 12 Apr
    A big thank you for your blog article.Thanks Again. Want more.
  54. 54 email@gmail.com 12 Apr
    Thanks for sharing, this is a fantastic post. Want more.
  55. 55 jymmijons@gmail.com 12 Apr
    I am so grateful for your article post.Really thank you! Much obliged.
  56. 56 Wirtschaftsfachwirt 12 Apr
    Great blog post.Really thank you! Want more.
  57. 57 email@gmail.com 12 Apr
    I appreciate you sharing this post. Awesome.
  58. 58 greg.ham@gmail.com 12 Apr
    I really enjoy the blog post.Thanks Again. Great.
  59. 59 cheap term life insurance 12 Apr
    Wow, great post.Really thank you!
  60. 60 email@gmail.com 12 Apr
    Im grateful for the blog post. Will read on...
  61. 61 golojan@gmail.com 12 Apr
    Im grateful for the article. Want more.
  62. 62 sex gratis 12 Apr
    Muchos Gracias for your blog.Really looking forward to read more.
  63. 63 email@gmail.com 12 Apr
    Thanks for sharing, this is a fantastic article post.Really thank you! Really Cool.
  64. 64 golojan@gmail.com 12 Apr
    Fantastic article.Really thank you! Great.
  65. 65 cannabis effects 12 Apr
    Fantastic blog article.Thanks Again. Keep writing.
  66. 66 emai1l@gmail.com 12 Apr
    I really enjoy the blog.
  67. 67 backlinksmaster 12 Apr
    This is one awesome article.Really looking forward to read more. Keep writing.
  68. 68 email@gmail.com 12 Apr
    Really informative article.Really looking forward to read more. Want more.
  69. 69 super sorbent 12 Apr
    Thanks so much for the blog post.Thanks Again. Want more.
  70. 70 email@gmail.com 12 Apr
    Great blog post.Really thank you! Keep writing.
  71. 71 susanzz@gmail.com 12 Apr
    I appreciate you sharing this blog post.Really looking forward to read more. Much obliged.
  72. 72 stretch mark removal 12 Apr
    Im obliged for the blog article.Really looking forward to read more. Really Great.
  73. 73 email@gmail.com 12 Apr
    Thanks for sharing, this is a fantastic blog post.Much thanks again. Great.
  74. 74 susanzz@gmail.com 12 Apr
    Appreciate you sharing, great blog post.Really looking forward to read more. Awesome.
  75. 75 Droid battery 12 Apr
    I value the post.Really looking forward to read more. Really Great.
  76. 76 henryfrod@gmail.com 12 Apr
    I think this is a real great blog article.Much thanks again. Fantastic.
  77. 77 goldfish care tips 12 Apr
    This is one awesome article.Really looking forward to read more. Want more.
  78. 78 howardlee@gmail.com 12 Apr
    I truly appreciate this blog.Much thanks again. Really Cool.
  79. 79 henryfrod@gmail.com 12 Apr
    I value the blog article. Really Great.
  80. 80 huiztheman@gmail.com 12 Apr
    Fantastic post. Will read on...
  81. 81 Shopping Cart Webdesign Sydney 25 Apr
    Wow, It,s quite interesting Blog, having nice information. Sometimes we ignore this sort of things & also suffer a lot as well. However we can
    save a lot with the assistance of these tips for example time etc.
  82. 82 Professional Website Design 25 Apr
    Wow, It,s quite interesting Blog, having nice information. Sometimes we ignore this sort of things & also suffer a lot as well. However we can save a lot with the assistance of these tips for example time etc.
  83. 83 Cabana 25 Apr
    I liked this article so much because the subject is chosen here is so much intresting.Hope everybody would like this this one.
  84. 84 Poolside Cabanas 25 Apr
    I liked this article so much because the subject is chosen here is so much intresting.Hope everybody would like this this one.
  85. 85 Erotic Show Reviews 26 Apr
    It's quite interesting blog that contain good information about theme.Realy, Great blog and thanks for the great article.
  86. 86 Los Angeles Furniture Stores 26 Apr
    It's quite interesting blog post that realy i like.Because your article contain having good knowledge of them.

Comment

  1.