I am using an ItemsCollection to build a navigation bar in my application. The Navigation bar has a combobox for the "address" that will track the history of a users movement through the system. I have a dependency property of time itemscollection to sync the currently selected portion of my applciation to the navigation bar's combo box. This is working well, except I found one limitation. I can set the itemsource of my combobox to the ItemsCollection Dependency Property and use a DataTemplate to properly display the underlying objects in the list. However, I wanted to bind the text property of the combobox to the first item in the ItemsCollection Dependency Property. This seemed easy enough, the binding statement shoud be myDepObj.Item(0).myProperty. In fact, I can do this in the code behind on a button press (set text equal to this), however binding it doesn't work.
I struggled with this for hours and could not make it work no matter what I tried. Then I read this article at Dr. WPF which states the following:
The Items property is not backed by a dependency property.
This means that you cannot set a binding directly on the Items property. However, you can definitely bind an ItemsControl to a collection of items
I learned this the hard way. So basically, when navigation occurs in code the list of items in the combobox is updated automatically as its itemsource is set to my itemscollection dependency property, but unfortunately the text is not automatically set to the top item in my itemsource the way I want. I even tried binding the text property of the combobox to the combobox's own Items(0).URL property but that didn't work either. Again, an individual item is NOT backed by a dependency property so it does not know to update the text property automatically. Another promising approach that failed was to try to update the text property to the top item on the combobox's "sourceupdated" or the "targetupdated" events. Alas these never fired when the underlying dependency object was modified.
I then discovered a simpler way to do this....Simply setting the property IsSynchronizedWithCurrentItem on the combobox and always setting the current position on the underlying ItemsCollection the the first item acheived exactly what I wanted. However, the combobox's selected item is not displaying the field in my datatemplate, it is instead showing the name of my object (this night is never going to end). This is because the datatemplate is only being applied for a TextBlock which is readonly, however my combobox has IsEditable set to true so the portion of the combobox that makes up the item is not a textblock but a textbox. Fortunately I found this forum posting at MSFT where they recommend overriding the tostring method to display what I want to display. This worked great as I was able to display one thing in the drop down part and another thing in my textbox!
Hope this helps someone else avoid a long frustrating night like the one I've had :)