动态地将事件分配给新的UIElement以处理拖放

时间:2011-08-04 14:07:48

标签: wpf events drag-and-drop uielement

我的问题是我正在尝试为用户创建一个工具箱,这样他就可以创建单选按钮,组合框等,然后这些创建的元素可以拖放到画布或其他任何内容中。

实际上我可以管理以前由我创建的元素的拖放,现在问题出现在用户创建元素时,我在设置动态分配事件以处理拖放时遇到问题。

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

我的工具箱是列表框,因此当用户选择项目时,它会创建UI元素。

我会感激任何帮助。提前谢谢你,对我的英语感到抱歉!

1 个答案:

答案 0 :(得分:0)

听起来像附加行为的工作。拖动元素是一个常见的例子。这是一个:drag behaviour,这是另一个:Drag & Drop With Attached Properties

这应该可以整理一下你的代码。你只需要一个共同的位置来执行附加 - 可能只是一个方法或写一个ElementFactory类。