样品拖放行为的代码不工作
问题描述:
这是行为的复制从WPF的教程页粘贴:样品拖放行为的代码不工作
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 ()'永远不会被打/叫。所以拖动&拖放也不起作用。
答
Blend包含在VisualStudio2105中,并附带了交互式dll。如果使用路径:xmlns:d =“http://schemas.microsoft.com/expression/2010/intera ctivity”而不是xmlns:d =“http://schemas.microsoft.com/expression/blend/ 2008“OnAttached方法得到正确调用。
Blend包含在VisualStudio2105中,并附带交互式dll。如果使用路径: 'xmlns:d =“http://schemas.microsoft.com/expression/2010/interactivity”' 而不是 'xmlns:d =“http://schemas.microsoft.com/表达/混合/ 2008“' OnAttached方法得到正确调用。 – manni
请把它写成答案,所以我可以接受它作为答案,你得到+25代表。 –