在运行时为新UIelements设置事件

时间:2011-08-03 20:42:54

标签: wpf drag-and-drop uielement

我对某些问题感到困惑,我正在做一个用户应该可以使用单选按钮,组合框等设计问题的项目(有点像VS10的工具箱来设计你的XAML)。

到目前为止,我可以拖放我之前创建的UIElement,当用户从我的工具箱中创建新元素时出现问题,我无法找到制作新UIElement的方法从我最初创建的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;
    }
    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();
    }

}

谢谢!

1 个答案:

答案 0 :(得分:1)

如果您只想在新RadioButton处理鼠标,请更改此行:

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

对此:

newRadio.PreviewMouseDown += generic_PreviewMouseDown;

修改

然后您需要将generic_PreviewMouseDown更改为以下内容:

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