My WPF Custom Control handles dragging shapes across an image on a canvas. Some shapes may be dragged beyond the limits of the image but others should be limited. But the control itself does not know the rules, only clients (i.e. the XAML page that created the control or its View-model) knows what they are.
So I need to give this control a way to let clients know that shapes are about to be dragged so that clients may limit drag for the duration of the drag operation
I had the idea for a RoutedEvent
(e.g. ItemDragStarting
) that my control could raise when shape dragging begins. I reasoned that we already know that event handlers traditionally set the RoutedEventArgs.Handled
property to stop further processing of the event. That seems basically like an "OUT" parameter doesn't it?
So maybe my RoutedEventArgs
object of my ItemDragStarting
event could expose a "ValidLimitRect" parameter to allow clients to optionally set the valid limits. After the event fired, the control could check this and, if set, limit the drag for this one drag operation.
For example:
// User has started dragging items. Raise the event with no limit rect
var args = new DragStartingEventArgs(itemsBeingDragged, Rect.Empty);
RaiseEvent(args)
// Did any of the clients set the limit rect to anything?
if (!args.LimitRect.IsEmpty)
{
// Save the value to limit dragging
}
But I've never before made event-firing code actually care about the end state of its arguments. Until now they've always been fire-and-forget. So I am wondering if this is even a good idea.
Is this anything resembling standard practice in WPF or is this code-smell? If it is the latter, what's the better way to do this?