Search
Thursday, November 20, 2008 ..:: Blendblog.net ::.. Register  Login
 Bloggers Minimize

 Print   
 Sponsors Minimize

 Print   
 Users Online Minimize
Membership Membership:
Latest New User Latest: areel
New Today New Today: 0
New Yesterday New Yesterday: 0
User Count Overall: 15

People Online People Online:
Visitors Visitors: 0
Members Members: 0
Total Total: 0

Online Now Online Now:

 Print   
 Welcome to blendblog.net! Minimize

If you are a developer or designer who is creating cutting edge user interface experiences using Microsoft Expression Blend you've just found a great resource!  The purpose of this site is to share our experience with this brand new product.  As with any new piece of software there is a ton to discover.  Bugs will be revealed and workarounds discovered, hopefully this can be a resource to share in the discovery process!

While this site will focus largely on Expression Blend, it will also encompass WPF, Visual Studio, Silverlight, and other .net 3.5 technologies as they relate to the creation of great software.

Please login using your Microsoft LiveID or register a new account.  Doing so will let you comment on any of the blog entries on the site.  We look forward to helping you in your endeavors as well as you helping others in theirs!  We'd love to have more bloggers on our site...if you are interested please register and contact us!


 Print   
 Important Fact about the ItemsCollection and fun with comboboxes Minimize
Location: BlogsSean Cullinan    
Posted by: Sean Cullinan 1/24/2008 9:42 PM

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 :)

Permalink |  Trackback

Your name:
Title:
Comment:
Add Comment   Cancel 

  
 Search Minimize

 Print   
 Archive Minimize

 Print   
 Blendblog Survey Minimize
Our next client application will be written using:




Submit Survey  View Results

 Print   
Copyright 2008 by blendblog.net   Terms Of Use  Privacy Statement
DotNetNuke® is copyright 2002-2008 by DotNetNuke Corporation