2

My problem is that I'm trying to do a toolbox for the user so he can create radio buttons, combo box, etc and then those created elements be able to drag and drop inside a canvas or whatever.

Actually I can manage drag and drop from a element previously created by me, now the problem comes when the user creates an element, I'm having problem assigning the events dynamically to handle drag and drop.

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Canvas Height="190" HorizontalAlignment="Left" Margin="158,41,0,0" Name="canvas1" VerticalAlignment="Top" Width="322" AllowDrop="True">
        <Button Content="PROBANDO" Height="23" Name="button" Width="75" Canvas.Left="113" Canvas.Top="43" PreviewMouseDown="button_PreviewMouseDown" PreviewMouseMove="button_PreviewMouseMove" MouseUp="button_MouseUp" IsEnabled="True" />
        <TextBlock Canvas.Left="99" Canvas.Top="147" Height="23" Name="textBlock" Text="" Width="107" />
    </Canvas>
    <ListBox Height="190" Name="listBox" Width="126" Margin="12,41,365,80" >
        <ListBoxItem Content="Radio Button" Selected="radio_Selected" Name="radio" />
        <ListBoxItem Content="Text" Selected="text_Selected" Name="text" />
        <ListBoxItem Content="Combo Box" Name="combo" Selected="combo_Selected" />
    </ListBox>
</Grid>
</Window>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    Point p;

    private void button_MouseUp(object sender, MouseButtonEventArgs e)
    {
        button.ReleaseMouseCapture();
    }

    private void button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        button.CaptureMouse();
        p = e.GetPosition(canvas1);
    }

    private void button_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        Point x = e.GetPosition(canvas1);
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            Canvas.SetLeft(button, Canvas.GetLeft(button) + (x.X - p.X));
            Canvas.SetTop(button, Canvas.GetTop(button) + (x.Y - p.Y));
        }
        p = x;
    }

    // Generic event to handle drag and drop on a new UIElement creadted by user
    private void generic_PreviewMouseDown(UIElement sender, MouseEventArgs e)
    {
        Point x = e.GetPosition(canvas1);
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            Canvas.SetLeft(sender, Canvas.GetLeft(sender) + (x.X - p.X));
            Canvas.SetTop(sender, Canvas.GetTop(sender) + (x.Y - p.Y));
        }
        p = x;
    }

    // Creates a new radio button and assign the events to handle drag and drop 
    private void radio_Selected(object sender, RoutedEventArgs e)
    {
        RadioButton newRadio = new RadioButton();
        canvas1.Children.Add(newRadio);
        newRadio.PreviewMouseDown += generic_PreviewMouseDown(newRadio, MouseEventArgs);
        textBlock.Text = listBox.SelectedIndex.ToString();
    }

    private void text_Selected(object sender, RoutedEventArgs e)
    {
        TextBox newText = new TextBox();
        canvas1.Children.Add(newText);
        textBlock.Text = (String)listBox.SelectedIndex.ToString();
    } 

    private void combo_Selected(object sender, RoutedEventArgs e)
    {
            Console.Write("Combo");

        textBlock.Text = (String)listBox.SelectedIndex.ToString();
    }
}

My toolbox is the listbox so when the user select an item it creates the UI element.

I'll appreciate any help. Thank you in advance and sorry about my English!

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Luis
  • 21
  • 2

1 Answers1

0

Sounds like a job for an attached behaviour. Dragging elements around is a common example. Here is one: drag behaviour and here is another: Drag & Drop With Attached Properties.

This should tidy up your code a bit. You just need a common place to perform the attach - maybe just a method or write an ElementFactory class.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
David Hollinshead
  • 1,710
  • 17
  • 18