1 2 3 4 5 下一页 在Silverlight中自身并没有提供拖放功能的相关实现,要实现拖放功能得借助其事件支持(MouseLeftButtonDown、 MouseLeftButtonUp和MouseMove)来完成,实际应用中我们可以通过行为(Behavior)特性将拖放操作封装为行为,这样可达到代码复用的效果。而在Blend中则直接提供了拖放操作行为,它位于Microsoft.Expression.Interactions.dll的 Microsoft.Expression.Interactivity.Layout名称空间下。 Silverlight中的拖放操作通常是使用事件驱动动态定位对象的坐标来实现,首先来看看如何通过代码的可编程方式在Silverlight中实现拖放操作,如下代码块: private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { FrameworkElement element = sender as FrameworkElement; MousePosition = e.GetPosition(null); IsMouseCaptured = true; element.CaptureMouse(); element.Cursor = Cursors.Hand; } private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { FrameworkElement element = sender as FrameworkElement; IsMouseCaptured = false; element.ReleaseMouseCapture(); MousePosition.X = MousePosition.Y = 0; element.Cursor = null; } private void OnMouseMove(object sender, MouseEventArgs e) { FrameworkElement element = sender as FrameworkElement; if (IsMouseCaptured) { double Y = e.GetPosition(null).Y - MousePosition.Y; double X = e.GetPosition(null).X - MousePosition.X; X = X + (double)element.GetValue(Canvas.LeftProperty); Y = Y + (double)element.GetValue(Canvas.TopProperty); element.SetValue(Canvas.LeftProperty, X); element.SetValue(Canvas.TopProperty, Y); MousePosition = e.GetPosition(null); } }
(责任编辑:admin) |