tags:

views:

476

answers:

3

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

http://stackoverflow.com/questions/32541/how-can-you-clone-a-wpf-object

A: 

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.

davogones
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
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
A: 

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.

Jobi Joy
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
That is my answer , You have to create a UserControl with those items inside.
Jobi Joy
A: 

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: http://www.nbdtech.com/blog/archive/2007/08/23/WPF-Image-Viewer-Part-4-Data-Templates-and-Resources.aspx

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:

  1. Set an object with all the form data as the Window's DataContext, I'll class this class PostDelvertData.
  2. Create another class with the repeating data, I'll call is ChildDeliveryData
  3. Add a property of type ObservableCollection called AllChildren to PostDeliveryData, it's important it'll be ObservableCollection and not any other type of collection.
  4. 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.

Nir
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
its really a bit confusing any sample application before i blow my brains thinking and trying to figure out
thank you will try and let u know the result
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/ajyvnwmy fren suggested using control library please assist.
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