我创建了自己的控件,该控件是使用OnDrop方法继承自面板控件的,用于处理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;
}
}
[当我在面板上放置一个按钮并在拖放过程中将其移动并拖放到该面板上时,将触发两次放置事件。
只有一个放置事件,我该怎么办?
<Grid Background="AliceBlue">
<local:Panel_my Background="AliceBlue" MinDate="07:00" MaxDate="20:00" AllowDrop="True">
<local:Button_my local:Panel_my.StartDate="11:00" local:Panel_my.EndDate="13:00">
test
</local:Button_my>
</local:Panel_my>
</Grid>
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class Button_my : Button
{
public Button_my() { }
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.LeftButton == MouseButtonState.Pressed)
{
DragDrop.DoDragDrop(this, this.Content, DragDropEffects.Move);
}
}
}
public partial class Panel_my : Panel
{
public Panel_my()
{ }
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;
}
}