0

I'm kinda confused with some problem, I'm doing a project where the user should be able to design questions with radio buttons, combo box, etc (kinda like toolbox from VS10 to design your XAML).

So far I can drag and drop an UIElement that I previously created, problem comes when the user creates a new element from my toolbox, I can't find the way to make that new UIElement to get the same events from my previosly created UIElement. Take a look at the code

<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;
    }
    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;
    }


    private void radio_Selected(object sender, RoutedEventArgs e)
    {
        RadioButton newRadio = new RadioButton();
        canvas1.Children.Add(newRadio);
        newRadio.PreviewMouseDown += generic_PreviewMouseDown(newRadio,?????);
        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();
    }

}

Thanks!

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Luis
  • 21
  • 2
  • Are you asking how to copy a set of handlers for an event from one UIElement to another? For example, to copy all of the Click handlers? – Ed Bayiates Aug 03 '11 at 20:46
  • Mmm no, i want t know how can i dinamically (on runtime) give to a new created element the drag and drop throught events (previewMouseDown, etc) i've made the "generic_PreviewMouseDown" to assing it to he new element on his previewMouseDown event, but i failed at that as you can see in my code – Luis Aug 04 '11 at 15:00
  • You said no, and then yes, so I am confused. I didn't mean specifically the click handlers, I mean are you trying to copy event handlers _such as click_. – Ed Bayiates Aug 04 '11 at 15:03
  • If that is the way to do it then yes, right now i have the "generic_PreviewMouseDown" method wich i made to handle the drag and drop for a new created element by the user, but i need to link the event previewMousedown from then new element with this generic method i made. I hope you get my point – Luis Aug 04 '11 at 15:21
  • OK. Assuming that you know what you assigned, Abe's answer is the correct one. – Ed Bayiates Aug 04 '11 at 15:23

1 Answers1

1

If all you want to do is handle the mouse down on the new RadioButton, change this line:

newRadio.PreviewMouseDown += generic_PreviewMouseDown(newRadio,?????);

To this:

newRadio.PreviewMouseDown += generic_PreviewMouseDown;

Edit

And then you need to change the generic_PreviewMouseDown to the following:

private void generic_PreviewMouseDown(object sender, MouseEventArgs e)
{
    UIElement elem = sender as UIElement;

    Point x = e.GetPosition(canvas1);
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        Canvas.SetLeft(elem, Canvas.GetLeft(elem) + (x.X - p.X));
        Canvas.SetTop(elem, Canvas.GetTop(elem) + (x.Y - p.Y));
    }
    p = x;
}
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Abe Heidebrecht
  • 30,090
  • 7
  • 62
  • 66
  • Yes but the problem is that generic_PreviewMouseDown recieves 2 parameters so it can do the drag and drop generic_PreviewMouseDown(UIElement sender, MouseEventArgs e) – Luis Aug 04 '11 at 14:56
  • Right, I didn't look closely at the generic_PreviewMouseDown method. I figured you had used the correct method signature for the event. I've updated my answer to show you how to do it. – Abe Heidebrecht Aug 04 '11 at 15:15
  • I see, but when i make the call, after i created the new radio button: newRadio.PreviewMouseDown += generic_PreviewMouseDown(newRadio,?????); it needs a MouseEventArg for his 2nd parameter, how can make that call giving it that parameter so the drag and drop can still work, thank you again – Luis Aug 04 '11 at 15:34
  • Well, when you assign an event handler through +=, you are saying that when the PreviewMouseDown event happens, it should call your method. Since the PreviewMouseDown event has a compatible signature, it will send the proper arguments to your method when the mouse button is pressed on the new RadioButton. If you want to read up on events in .NET: http://msdn.microsoft.com/en-us/library/17sde2xt.aspx – Abe Heidebrecht Aug 04 '11 at 15:40
  • Thank you man, i think i got it, i'll read your link right away. BTW when i try to make this call newRadio.PreviewMouseDown += generic_PreviewMouseDown; it wont let me cause is expecting generic_PreviewMouseDown 2 parameters, i have the UIElement but the MouseEventArg i don't. You said that the += will provide the arguments but it is asking for arguments before i can run it. – Luis Aug 04 '11 at 16:07
  • When you do the += assignment, you shouldn't use parenthesis or arguments. You need to modify your code to exactly what I have provided in the answer. The += is the same as setting it in the XAML (PreviewMouseDown="generic_PreviewMouseDown"). You aren't actually calling the method at that point, just registering it with the event handler to be called when the event is raised. – Abe Heidebrecht Aug 04 '11 at 16:55