C# Drop事件在继承的面板上激发两次

C# Drop事件在继承的面板上激发两次,c#,C#,我用OnDrop方法创建了从panel控件继承来的自己的控件,用于处理Drop事件。不幸的是,事件被触发了两次。 我不知道为什么 这是我的密码 public partial class Panel_my : Panel { public static readonly DependencyProperty StartDateProperty = DependencyProperty.RegisterAttached("StartDate", type

我用OnDrop方法创建了从panel控件继承来的自己的控件,用于处理Drop事件。不幸的是,事件被触发了两次。 我不知道为什么

这是我的密码

public partial class Panel_my : Panel
    {
        public static readonly DependencyProperty StartDateProperty =
           DependencyProperty.RegisterAttached("StartDate", typeof(DateTime), typeof(Panel_my), new FrameworkPropertyMetadata(DateTime.MinValue, FrameworkPropertyMetadataOptions.AffectsParentArrange));
        public static readonly DependencyProperty EndDateProperty =
            DependencyProperty.RegisterAttached("EndDate", typeof(DateTime), typeof(Panel_my), new FrameworkPropertyMetadata(DateTime.MaxValue, FrameworkPropertyMetadataOptions.AffectsParentArrange));

        public static readonly DependencyProperty MaxDateProperty =
           DependencyProperty.Register("MaxDate", typeof(DateTime), typeof(Panel_my), new FrameworkPropertyMetadata(DateTime.MaxValue, FrameworkPropertyMetadataOptions.AffectsMeasure));
        public static readonly DependencyProperty MinDateProperty =
            DependencyProperty.Register("MinDate", typeof(DateTime), typeof(Panel_my), new FrameworkPropertyMetadata(DateTime.MaxValue, FrameworkPropertyMetadataOptions.AffectsMeasure));


    public static DateTime GetStartDate(DependencyObject obj)
    {
        return (DateTime)obj.GetValue(StartDateProperty);
    }

    public static void SetStartDate(DependencyObject obj, DateTime value)
    {
        obj.SetValue(StartDateProperty, value);
    }

    public static DateTime GetEndDate(DependencyObject obj)
    {
        return (DateTime)obj.GetValue(EndDateProperty);
    }

    public static void SetEndDate(DependencyObject obj, DateTime value)
    {
        obj.SetValue(EndDateProperty, value);
    }

    public DateTime MaxDate
    {
        get { return (DateTime)GetValue(MaxDateProperty); }
        set { SetValue(MaxDateProperty, value); }
    }

    public DateTime MinDate
    {
        get { return (DateTime)GetValue(MinDateProperty); }
        set { SetValue(MinDateProperty, value); }
    }

    protected override Size MeasureOverride(Size availableSize)
    {
        foreach (UIElement child in Children)
        {
            child.Measure(availableSize);
        }

        return new Size(0, 0);
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        double range = (MaxDate - MinDate).Ticks;
        double pixelsPerTick = finalSize.Width / range;

        foreach (UIElement child in Children)
        {
            ArrangeChild(child, MinDate, pixelsPerTick, finalSize.Height);
        }

        return finalSize;
    }

    private void ArrangeChild(UIElement child, DateTime minDate, double pixelsPerTick, double elementHeight)
    {
        DateTime childStartDate = GetStartDate(child);
        DateTime childEndDate = GetEndDate(child);
        TimeSpan childDuration = childEndDate - childStartDate;

        double offset = (childStartDate - minDate).Ticks * pixelsPerTick;
        double width = childDuration.Ticks * pixelsPerTick;

        child.Arrange(new Rect(offset, 0, width, elementHeight));
    }

    protected override void OnDrop(DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.StringFormat))
        {

        }
        e.Handled = true;

    }

}
当我在面板上放置一个按钮并在拖放过程中移动它并将其拖放到该面板上时,将触发两次拖放事件


我该怎么做才能只举办一次投球活动

我明白了。问题在于,该按钮不仅在鼠标移动时触发事件MouseMove,而且在不移动鼠标的情况下按下左键时也会触发事件MouseMove。 我改变了MouseMove的方法,但效果更好

protected override void OnMouseMove(MouseEventArgs e)
{
    base.OnMouseMove(e);
    if (e.LeftButton == MouseButtonState.Pressed && this.IsPressed == false)
    {
        DragDrop.DoDragDrop(this, this.Content, DragDropEffects.Move);
    }
}

谢谢,@jdweng非常感谢您的帮助和指导

查看designer.cs文件以注册事件(+=')。您必须注册面板和继承面板。我不使用+=注册任何处理程序。在inherit面板中有OnDrop方法。此方法被调用两次,因为拖动事件有两次。必须注册所有事件!!!看看designer.cs文件!!!我浏览了所有.designer.cs和其他.cs文件。没有注册+=您需要一个+=才能使事件工作。它们可以在设计器或常规cs文件中。所以你不能参加活动。如果你要参加活动,你可以在活动中加入一个断点来验证。您还可以查看调用堆栈,了解您是如何进入事件的。