DataTemplate Bindings to Expando properties generated at runtime
-
Sunday, April 15, 2012 2:43 AM
Scenario:
Lets see, how can I explain this. I have a gridview with columns generated at runtime, each column may or may not have a celltemplate assigned to it. Basically think of it as if I were using a CellTemplateSelector for the gridview. The objects in the bound observablecollection contain dynamic objects who's properties are generated at runtime. It is possible for a property of a dynamic object to contain another dynamic object. It is the structure of the dynamic objects that determines whether or not a column in my gridview uses a celltemplate, i.e. if a property contains another dynamic object, then the column for that property uses a cell template.
Issue:
Now because these objects are created at runtime, I do not know the names of the dynamic objects that can potentially be a property on the dynamic object bound in the observable collection, therefore I can not define the bindings of the elements of the datatemplate in xaml. Here is the datatemplate:
<DataTemplate x:Key="BaseClassTemplate2"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Image Name="imgIcon" Source="{Binding Path=Image}" Grid.Column="0" Grid.RowSpan="2" Width="25" Height="25" Margin="5,0" /> <TextBlock Name="tb1" Grid.Column="1" Grid.Row="0" Text="{Binding Path=Name}" FontWeight="Bold" /> <TextBlock Name="tb2" Grid.Column="1" Grid.Row="1" Text="{Binding Path=Description}" TextWrapping="Wrap" /> </Grid> </DataTemplate>In order for me to bind to the properties of the dynamic object I would have to for example:
[name of property].Image, [name of property].Name, and [name of property].Description...
Now I didnt come right out and say this, but to put it in more straight forward terms, I need to set the binding of the 3 framework elements in the datatemplate at runtime.
I have tried a couple techniques without success. First I have tried using (dataTemplate.FindName("imgIcon",templatedParent) as Image).SetBinding(...) however I cant for the life of me determine the "templatedParent".
What I need:
I would like to set the bindings of the 3 framework elements in the datatemplate at the time that I create my column, before the grid is bound.
Alternatively, and less desired, would be to set the bindings after the grid is bound, in the RowLoaded event.
Please help, I have spent far too long on trying to figure this out, and I am about to abandon it.
Thanks,
Stuck

