A.B.
A.B.

Reputation: 2470

Sample Drag&Drop Behaviour Code is not working

This is the behaviour copy pasted from wpf-tutorial page:

public class DragBehavior : Behavior<UIElement>
{
    private Point elementStartPosition;
    private Point mouseStartPosition;
    private TranslateTransform transform = new TranslateTransform();

    protected override void OnAttached()
    {
        Window parent = Application.Current.MainWindow;
        AssociatedObject.RenderTransform = transform;

        AssociatedObject.MouseLeftButtonDown += (sender, e) =>
        {
            elementStartPosition = AssociatedObject.TranslatePoint(new Point(), parent);
            mouseStartPosition = e.GetPosition(parent);
            AssociatedObject.CaptureMouse();
        };

        AssociatedObject.MouseLeftButtonUp += (sender, e) =>
        {
            AssociatedObject.ReleaseMouseCapture();
        };

        AssociatedObject.MouseMove += (sender, e) =>
        {
            Vector diff = e.GetPosition(parent) - mouseStartPosition;
            if (AssociatedObject.IsMouseCaptured)
            {
                transform.X = diff.X;
                transform.Y = diff.Y;
            }
        };
    }
}

And this is my test page:

<Page x:Class="WPFApp.Pages.BehaviorPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:WPFApp.Pages"
      xmlns:b="clr-namespace:WPFApp.Behaviors"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="BehaviorPage">

    <Grid>
        <StackPanel >
            <Border Background="LightBlue">
                <d:Interaction.Behaviors>
                    <b:DragBehavior/>
                </d:Interaction.Behaviors>

                <TextBlock Text="Drag me around!" Width="110" FontSize="14"/>
            </Border>
        </StackPanel>
    </Grid>
</Page>

When i want to debug 'OnAttached()' is never hit/called. So Drag&Drop also doesn't work.

Upvotes: 0

Views: 72

Answers (1)

manni
manni

Reputation: 319

Blend was included in VisualStudio2105 and comes with the interactivity dll. If you use the path: xmlns:d="http://schemas.microsoft.com/expression/2010/intera‌​ctivity" instead of xmlns:d="http://schemas.microsoft.com/expression/blend/2008" the OnAttached method gets called correctly.

Upvotes: 1

Related Questions