在运行时为新 UI 元素设置事件

发布于 2024-11-27 13:00:24 字数 3294 浏览 1 评论 0原文

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

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

}

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唠甜嗑 2024-12-04 13:00:24

如果您只想在新的 RadioButton 上处理鼠标按下的情况,请将此行更改

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

为:

newRadio.PreviewMouseDown += generic_PreviewMouseDown;

Edit

然后您需要更改 generic_PreviewMouseDown< /code> 改为以下内容:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文