首页 新闻 搜索 专区 学院

WPF在MainWindow中定义了MouseMove事件之后,自定义控件中的其它事件就变的无效了(附源码)求指点

0
悬赏园豆:30 [待解决问题]

在MainWindow中定义了MouseMove事件之后,自定义控件中的其它事件就变的无效了(附源码)求指点

可以运行,但运行后MainWindow中的所有控件都可拖动,但是像TextBlock拖动到其它控件上时,其数据无法传到其它控件上,如果把MainWindow中的拖动事件去掉,就可以,求高手指点一下,万分感谢,在线等待当中......

自定义控件源码:

public partial class Box : UserControl
    {
        public Box()
        {
            InitializeComponent();
        }
        protected override void OnDragEnter(DragEventArgs e)
        {
            base.OnDragEnter(e);
            collections = MyBox.Children;
            if (e.Data.GetDataPresent(DataFormats.StringFormat)) {
                string data_string = (string)e.Data.GetData(DataFormats.StringFormat);
                TextBlock tb = new TextBlock();
                tb.Text = data_string;
                MyBox.Children.Add(tb);
            }
        }
        protected override void OnGiveFeedback(GiveFeedbackEventArgs e)
        {
            base.OnGiveFeedback(e);
        }
        protected override void OnDrop(DragEventArgs e)
        {
            base.OnDrop(e);
            if (e.Data.GetDataPresent(DataFormats.StringFormat))
            {
                string dataString = (string)e.Data.GetData(DataFormats.StringFormat);

                TextBlock tb = new TextBlock();
                tb.Text = dataString;
                MyBox.Children.Add(tb);

                if (e.KeyStates.HasFlag(DragDropKeyStates.ControlKey))
                {
                    e.Effects = DragDropEffects.Copy;
                }
                else
                {
                    e.Effects = DragDropEffects.Move;
                }
            }
            e.Handled = true;
        }
        protected override void OnDragOver(DragEventArgs e)
        {
            base.OnDragOver(e);
            if (e.Data.GetDataPresent(DataFormats.StringFormat))
            {
                string dataString = (string)e.Data.GetData(DataFormats.StringFormat);
                if (e.KeyStates.HasFlag(DragDropKeyStates.ControlKey))
                {
                    e.Effects = DragDropEffects.Copy;
                }
                else
                {
                    e.Effects = DragDropEffects.Move;
                }
            }
            e.Handled = true;
        }
        protected override void OnDragLeave(DragEventArgs e)
        {
            base.OnDragLeave(e);
            MyBox.Children.Clear();
            foreach (UIElement item in collections) {
                MyBox.Children.Add(item);
            }
           
        }

        private UIElementCollection collections;
    }

然后主窗口程序:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void MyCanvas_LeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement start = sender as FrameworkElement;
            FrameworkElement source = e.Source as FrameworkElement;
            FrameworkElement originalsource = e.OriginalSource as FrameworkElement;
            
            this.TargetCanvas = e.Source as FrameworkElement;
            this.ClickPoint = e.GetPosition(MyCanvas);
            this.MyText.Text = "StartPoint.X:" + this.ClickPoint.X + "StartPoint.Y" + this.ClickPoint.Y;

            OriginalLeft = Canvas.GetLeft(TargetCanvas);
            OriginalTop = Canvas.GetTop(TargetCanvas);
        }

        private void MyCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.TargetCanvas = null;
        }

        private void MyCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed) {
                this.DropPoint = e.GetPosition(MyCanvas);

                Canvas.SetLeft(this.TargetCanvas,OriginalLeft+this.DropPoint.X-this.ClickPoint.X);
                Canvas.SetTop(this.TargetCanvas,OriginalTop+this.DropPoint.Y-this.ClickPoint.Y);
            }
        }

        private FrameworkElement TargetCanvas;
        private TextBlock SelectTextBlock;
        private Point ClickPoint;
        private Point DropPoint;
        private double OriginalLeft;
        private double OriginalTop;
    }
高明无思的主页 高明无思 | 初学一级 | 园豆:6
提问于:2014-01-21 18:30
< >
分享
所有回答(1)
0

个人认为window是全局的 控件是局部的 当你拖动控件的时候也触发window的 俩者冲突 内部机制就不知道了 你可以在window事件里面判断你想用的某个控件是否获取焦点再执行控件的事件

c#牛刀小试 | 园豆:62 (初学一级) | 2014-08-18 11:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册