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.

Parents
  • 12631
    Verified Answer
    posted

    I would look at using the grids type matching feature to solve this problem.  the feature allows you to tell the grid to select a column layout to use based on the object type in the bound items source.  So for example, say I have two classes, ClassA and ClassB, each having different properties.  I can create two different ColumnLayouts and add them to the XamGrids ColumnLayouts collection (see the snippet below) and provide them with the type names.  At runtime the grid will select the ColumnLayout to use by matching the type of the items source to the type specified on the ColumnLayout.

    <ig:XamGrid x:Name="xamGrid1">
      <ig:XamGrid.ColumnLayouts>

        <ig:ColumnLayout Key="Layout1" TargetTypeName="ClassA">
          <ig:ColumnLayout.Columns>
            <ig:TextColumn Key="PropA" />
            <ig:TextColumn Key="PropB" />
            <ig:TextColumn Key="PropC" />
          </ig:ColumnLayout.Columns>
        </ig:ColumnLayout>

        <ig:ColumnLayout Key="Layout2" TargetTypeName="ClassB">
          <ig:ColumnLayout.Columns>
            <ig:TextColumn Key="PropA" />
            <ig:TextColumn Key="PropB" />
            <ig:TextColumn Key="PropC" />
            <ig:TextColumn Key="PropD" />
            <ig:TextColumn Key="PropE" />
            <ig:TextColumn Key="PropF" />
          </ig:ColumnLayout.Columns>
        </ig:ColumnLayout>

      </ig:XamGrid.ColumnLayouts>
    </ig:XamGrid>

    Note that the ColumnLayout still needs a Key, but in this scenario it just serves as a unique ID for the layout, its not tied to any propery of the ItemsSource.

    Hope that helps.

    Devin

Reply Children