【问题标题】:Drag and Drop WPF ComboBox, Buttons, Radio Buttons, etc拖放 WPF 组合框、按钮、单选按钮等
【发布时间】:2011-08-03 14:54:02
【问题描述】:

我有一个问题,我已经阅读了有关在 WPF 上拖放的教程、博客等(我使用的是 VS10)。

问题是我需要一个带有按钮、组合框、单选按钮等的工具箱,以便用户可以将其拖放(复制)到工作空间(画布或其他)上。

我设法从文本框和图像中拖放,但这对我不起作用,当我尝试按钮或组合框时它只是不起作用,我认为这是默认情况下单击事件的原因,我没有'不知道问题是什么。这是我用按钮尝试过的。

<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>
    <TextBox Height="22" HorizontalAlignment="Left" Margin="84,36,0,0" Name="textBox1" VerticalAlignment="Top" Width="103" Text="Drag" />
    <TextBox Height="40" HorizontalAlignment="Left" Margin="225,136,0,0" Name="textBox3" VerticalAlignment="Top" Width="124" Text="Drop" />
    <Label Content="DragLabel" Height="26" HorizontalAlignment="Left" Margin="284,36,0,0" Name="label1" VerticalAlignment="Top" Width="80" MouseDown="label1_MouseDown" />
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="84,122,0,0" Name="button1" VerticalAlignment="Top" Width="75" MouseDown="button1_MouseDown" AllowDrop="True" IsEnabled="True" Click="button1_Click" />
    <Rectangle Height="100" HorizontalAlignment="Left" Margin="149,199,0,0" Name="rectangle1" Stroke="Black" VerticalAlignment="Top" Width="200" AllowDrop="True" Fill="#FFDCA1A1" />
</Grid>

我的代码背后...

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

        private void label1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Label lbl = (Label)sender;
            DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Copy);
        }


        private void button1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var dependencyObject = (Button)sender;
            DragDrop.DoDragDrop(dependencyObject, dependencyObject, DragDropEffects.Move);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            return;
        }
    }

提前谢谢你们。顺便问一下我的英语:s...

再次感谢!

路易斯

【问题讨论】:

    标签: wpf drag-and-drop


    【解决方案1】:

    您是否尝试过使用PreviewMouseDown 事件而不是MouseDown?您的代码将在 Button 捕获点击之前被调用。

    WPF 元素通常使用RoutedEvents,它通常有一个使用Tunneling Routing Strategy 的相应“预览”事件,该事件将在实际引发事件的元素之前发送给所有父级。这允许您执行操作以响应MouseDownButton 有机会尝试执行点击操作。

    【讨论】:

      【解决方案2】:
      private void button1_PreviewMouseDown(object sender, MouseButtonEventArgs e)
      {
          var dependencyObject = (Button)sender;
          DragDrop.DoDragDrop(dependencyObject, dependencyObject, DragDropEffects.Move); 
      } 
      

      将按照 Abe

      所述工作

      【讨论】:

        猜你喜欢
        • 2011-01-26
        • 2018-06-18
        • 2011-11-03
        • 1970-01-01
        • 2021-08-30
        • 1970-01-01
        • 1970-01-01
        • 2013-03-31
        • 2013-02-28
        相关资源
        最近更新 更多