Posts Tagged DataBinding
ContentControl messing up with DataContext
Today I did binding between TreeView.ItemsSource and collection of items to display. I used HierarchicalDataTemplate to describe visualization of the each item in the collection. Inside template, I used ContentControl with was bind to object.Content property of the object (object from collection). What I wanted to do is to store inside tree node reference to object, for future use (handle item selected changed event). Since DataContext is always set by current binding source, I expected to use this for my purpose – to get reference to object from selected FrameworkElement.DataContext. However, this didn’t worked – inside DataContext of selected item was… object.Content (rather than object itself). After pretty long investigation, I noticed that TextBlock, for example, works as expected. So the problem is with ContentControl, which is assigning DataContext by bind property rather than by source of the property. I found in net this article, which is describing other problem but the same reason, and possible solution.
Add comment April 24, 2008