这是从wpf-tutorial页面粘贴的行为副本:
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;
}
};
}
}
这是我的测试页面:
<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>
当我想调试'OnAttached()时,永远不会被命中/调用。因此Drag&amp; Drop也不起作用。
答案 0 :(得分:1)
Bleild包含在VisualStudio2105中,并带有交互性dll。如果您使用路径:xmlns:d =&#34; http://schemas.microsoft.com/expression/2010/intera ctivity&#34;而不是xmlns:d =&#34; http://schemas.microsoft.com/expression/blend/2008"正确调用OnAttached方法。