Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2320
Dynamic Binding ItemsSource question
posted

I have a grid that has 10 Columns defined in it.  In code-behind I dynamically bind the ItemsSource to different collections.

One collection has all 10 columns.  Another collection only has 5 of them.

When the code switches to the collection with only 5 columns, I get this error

The following key(s) do not correspond with the DataSource etc, etc.

Because that column doesn't exist in the other collection.

I tried a hack.  I tried adding 1 column to the grid, and in codebehind, dynamically setting the Key to this one dummy property while setting visibility to collapsed.  It binds, but you get the weirdest glitch.  The grid does some visual stuff I can't even explain.

Can anyone think of a better way to accomplish this?  I considered adding fake properties that I can bind to but adding 5 fake properties to the underlying object is less than ideal if there is a better solution.  I could also have 2 grids, hiding or showing whichever but again, it gets messy. I could build the columns up dynamically also, but again, very very messy.

Any ideas would be appreciated.