I have the following code and basically what i am not able to figure out is how to clone the whole grid and make a blank copy of them side by side.... for a clear understanding this is something to do with hospital application and the grid is related to a pregnancy so when said 'ADD CHILD' button a whole new grid should be created during run time, thanks for the help below is a link that might help people cause i tried it but not sure how to display it
-
Unfortunately, none of the frameworks I have come across so far (both web and desktop) provide this option. I feel it would be very useful if implemented. – Agnel Kurian May 19 '09 at 12:39
-
Most answers provided here won't be of any use if the control to be cloned are being initialized by some third-party code. – Agnel Kurian May 19 '09 at 12:41
3 Answers
You should put the object you are want to "clone" in a DataTemplate
and reference this template from an ItemsControl
, then when you need another grid add another item to the items control (or even better to the list the control is bound to) and the ItemsControl
will create a new grid and bind it the new object.
For an example take a look at this post on my blog.
Here is an example for this application (I left only the relevant parts and I didn't test it, so there are probably some typos there):
<Window ... >
<Window.Resources>
<DataTemplate x:Key="ChildTemplate">
<Grid>
...
<TextBlock Text="Delivery Date:" Grid.Column="0" Grid.Row="0"/>
<TextBox Text="{Binding DeliveryDate}" Grid.Column="1" Grid.Row="0"/>
<TextBlock Text="Delivery Time:" Grid.Column="0" Grid.Row="1"/>
<TextBox Text="{Binding DeliveryTime}" Grid.Column="1" Grid.Row="1"/>
...
</Grid>
</DataTemplate>
</Window.Resources>
...
<Button Content="AddChild" Click="AddChildClick"/>
...
<ScrollViewer>
<ItemsControl ItemsSource="{Binding AllChildren}" ItemsTemplate="{StaticResource ChildTemplate}">
<ItemsControl.PanelTemplate>
<ItemsPanelTemplate><StackPanel Orientation="Horizontal"/></ItemPanelTemplate>
<ItemsControl.PanelTemplate>
</ScrollViewer>
...
</Window>
And in cs:
- Set an object with all the form data as the Window's
DataContext
. I'll call this classPostDelveryData
. - Create another class with the repeating data. I'll call it
ChildDeliveryData
. - Add a property of type
ObservableCollection<ChildDeliveryData>
calledAllChildren
toPostDeliveryData
; it's important it'll beObservableCollection
and not any other type of collection. Now, for the magic:
private void AddChildClick(object sender, RoutedEvetnArgs e) { ((PostDeliveryData)DataContext).AllChildren.Add(new ChildDeliveryData()); }
And when you add the new item to the list another copy of the entire data template will be added.

- 2,336
- 5
- 31
- 40

- 29,306
- 10
- 67
- 103
-
so using child grid control i will be able to make a whole new grid copy of the existing one?...http://www.flickr.com/photos/14529949@N07/3262167943/ so as from the diagram you can understand that i need to add this whole lot of controls if you like to see full project please let me know thankyou – Feb 08 '09 at 11:15
-
its really a bit confusing any sample application before i blow my brains thinking and trying to figure out – Feb 08 '09 at 11:16
-
hey NIR i tried what u said man but i find it a bit difficult using it not really getting the idea...gues i am a noob after all...please help got till firday to hand in... I have uploaded the file at the following link http://tinyurl.com/ajyvnw my fren suggested using control library please assist. – Feb 10 '09 at 14:27
-
Oh i forgot to say thanks before hand and also i know u r worried about the tinyurl could be virus u might be thinking please don't suspect me i did it as i am being counted of the characters thanks – Feb 10 '09 at 14:37
I'm not sure that you're using the correct approach here. I would approach the problem by creating a "ChildGridControl" with a Child property, and let the Child property handle the databinding. Adding a new child to the GUI would involve creating a new instance of the ChildGridControl.

- 7,321
- 31
- 36
-
so using child grid control i will be able to make a whole new grid copy of the existing one?...http://www.flickr.com/photos/14529949@N07/3262167943/ so as from the diagram you can understand that i need to add this whole lot of controls if you like to see full project please let me know thankyou – Feb 08 '09 at 10:35
-
Well, the point I was making was that you don't need to "copy" that child grid. If you encapsulate the child grid into a user control, you can add as many instances of the user control as you'd like. Another option is to only have one grid on the page, but populate it with a different Child. – davogones Feb 08 '09 at 19:06
If I am understanding correctly, you should create a UserControl, which wraps your Grid and subsequent controls inside. And use this User control anywhere you wanted to replicate that UI.

- 49,102
- 20
- 108
- 119
-
so using child grid control i will be able to make a whole new grid copy of the existing one?...http://www.flickr.com/photos/14529949@N07/3262167943/ so as from the diagram you can understand that i need to add this whole lot of controls if you like to see full project please let me know thankyou – Feb 08 '09 at 11:17
-
That is my answer , You have to create a UserControl with those items inside. – Jobi Joy Feb 08 '09 at 22:03