views:

224

answers:

2

How to drag and drop the dynamically created controls my code is shown below

 Button btnTask = new Button();
 btnTask.Content = _myCustomTasks[iCtr].Description;
 btnTask.Background = _myCustomTasks[iCtr].TaskColor;
 stackPanel.Children.Add(btnTask);

my requirement is to drag and drop these dynamically created button control. Thanks in Advance

A: 
  1. Controls dragged and dropped into the designer are not dynamicalled created. They are statically created and recreated. Theses controls are accessible from the toolbox in most cases.
  2. Controls declared in the code behind such as your Button are dynamically created. They MUST be created and recreated programmatically. Theses controls are exclusive to the code behind.

However, when using dynamically created controls it is a common practice to use PlaceHolders (this control is not rendered) to position your objects.

To do so...

  1. Drag and drop a PlaceHolder where you would like your button to be. (designer).
  2. Add your Button to the PlaceHolder programmatically. (Code Behind)

For instance...

Button btnTask = new Button();
btnTask.Content = _myCustomTasks[iCtr].Description;
btnTask.Background = _myCustomTasks[iCtr].TaskColor;
PlaceHolder1.Controls.Add(btnTask);

Hope it helps...

Maxime
A: 

My suggestion for drag-n-drop in silverlight:

  1. Define a Canvas layout 'below' your regular layout (z-order).

  2. When you select your object to drag-n-drop from your regular layout (grid, etc), move the object to the underlying Canvas which allows pixel positioning. Move the canvas 'above' the current layout.

  3. Drag the object by updating its Canvas position.

  4. When you finished the drag, return the object to the regular layout at the appropriate location (you might have to fish through your top-level layout to find the appropriate connection point). Return the canvas layer to a lower z-order than your regular layout.

This worked pretty well for a simple Drag-drop system that i needed.

Telerik controls also provides a drag-n-drop framework.

-Jeff

Jeff