处理鼠标事件并实现拖放操作肯定会有效,但根据您尝试的操作,您可以利用Expression Blend行为.Microsoft.Expression.Interactions DLL包含一些在Silverlight和WPF中使用的有用的基本行为,触发器和操作.
有一个MouseDragElementBehavior实现了一个元素的基本拖放功能,无论你的布局容器如何,它都应该有效(所以你不会被限制在Canvas中).您可以使用Blend将此行为放到元素上,或者如果您愿意,可以直接在XAML中定义它:
<Rectangle Fill="Red" Stroke="Black" HorizontalAlignment="Left" Width="100" Height="100">
<i:Interaction.Behaviors>
<il:MouseDragElementBehavior/>
</i:Interaction.Behaviors>
</Rectangle>
Run Code Online (Sandbox Code Playgroud)
您的项目必须引用System.Windows.Interactivity.dll和Microsoft.Expression.Interactions.dll才能使用此行为.
编辑(显示在C#代码隐藏中附加此行为):
Rectangle rect = new Rectangle();
rect.Fill = new SolidColorBrush(Colors.Red);
rect.Width = 100;
rect.Height = 100;
MouseDragElementBehavior dragBehavior = new MouseDragElementBehavior();
dragBehavior.Attach(rect);
Run Code Online (Sandbox Code Playgroud)
请记住在您的using语句中包含Microsoft.Expression.Interactivity.Layout命名空间.
归档时间: |
|
查看次数: |
7169 次 |
最近记录: |