views:

1101

answers:

1

I would like to have a Canvas that supports the DragDelta event. My first idea of how to do this was to make a ControlTemplate that includes a thumb. But I do not know how to do this correctly. How can I change the XAML below to make it compile, and what's the right way to install the DragDelta event handler?

<UserControl.Resources>
    <ControlTemplate x:Key="Transparent">
        <Rectangle Fill="Transparent"/>
    </ControlTemplate>
    <ControlTemplate x:Key="ThumbCanvas" TargetType="{x:Type Canvas}">
        <Grid>
            <Thumb x:Name="CanvasThumb" Template="{StaticResource Transparent}" DragDelta="CanvasThumb_DragDelta"/>
            <ContentPresenter Content="{TemplateBinding Content}"/>
            <!--error MC3011: Cannot find the static member 'ContentProperty' on the type 'Canvas'.-->
        </Grid>
    </ControlTemplate>
</UserControl.Resources>
+1  A: 

Any panel derived control doesn't have a template property, since they have no visible components.

You can use the Thumb control inside the UserControl, like you are doing and just adjust the Height/Width of the UserControl based on the delta of the Thumb. To make this work correctly, you need to get the mouse location relative to the parent of the usercontrol, otherwise it won't work well. ie: e.GetLocation(this.Parent);

You should probably size the height/width on that thumb as well. It's a good idea to set the cursor on it also.

mdm20
I assumed if I simply had a thumb underneath the canvas that the canvas would get the mouse event and not the thumb. No? BTW, I will not use the thumb for resizing, but merely to distinguish between clicking and dragging on the canvas.
Qwertie
It turns out Canvas doesn't receive mouse events (and they pass through) if it is not given a value for Background. Unfortunately, the standard MouseDown, MouseUp, MouseMove events aren't working correctly for me when attaching handlers to the Thumb...
Qwertie
Update: When attaching events to a Thumb, I have to handle PreviewMouseDown and PreviewMouseMove instead of MouseDown and MouseMove, as the Thumb blocks the regular events.
Qwertie