On advice from Twitter 🙂 I’m trying a new code-formatting plug-in for Live Writer. It’s this one from here.
Here’s an experimental piece of code that my previous plug-in wasn’t coping with very well, let’s see if this post shows up and whether it causes my blog engine to explode 🙂
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Expression = Microsoft.Expression.Interactivity; using System.Linq; using System.Windows.Data; using System.Collections.Generic; using System.Reflection; namespace MikesDragDropBits { public class DraggableBehavior : Expression.Behavior<FrameworkElement> { public DraggableBehavior() { } public string PropertyPath { get; set; } protected override void OnAttached() { base.OnAttached(); this.AssociatedObject.MouseLeftButtonDown += OnMouseDown; this.AssociatedObject.MouseLeftButtonUp += OnMouseUp; this.AssociatedObject.MouseMove += OnMouseMove; } protected override void OnDetaching() { base.OnDetaching(); this.AssociatedObject.MouseLeftButtonDown -= OnMouseDown; this.AssociatedObject.MouseLeftButtonUp -= OnMouseUp; this.AssociatedObject.MouseMove -= OnMouseMove; } void OnMouseMove(object sender, MouseEventArgs e) { if (isDragging) { if (injectedUI == null) { Point localPoint = e.GetPosition(this.AssociatedObject); Point globalPoint = e.GetPosition(Application.Current.RootVisual); capturePoint = globalPoint; globalPoint.X -= localPoint.X; globalPoint.Y -= localPoint.Y; injectedUI = new DragDropUIInjector(this.AssociatedObject, globalPoint); injectedUI.InjectUI(); } else { Point currentPoint = e.GetPosition(Application.Current.RootVisual); injectedUI.ApplyDelta(currentPoint.X - capturePoint.X, currentPoint.Y - capturePoint.Y); capturePoint = currentPoint; } } } void OnMouseUp(object sender, MouseButtonEventArgs e) { this.AssociatedObject.ReleaseMouseCapture(); isDragging = false; if (injectedUI != null) { injectedUI.RemoveUI(); injectedUI = null; HitTestAndInvokeTriggers(e.GetPosition(Application.Current.RootVisual)); } } void HitTestAndInvokeTriggers(Point p) { object dataObject = GetDataObject(); foreach (DropTrigger trigger in GetDropTriggersOfFirstElement(p)) { trigger.InvokeActions(dataObject); } } object GetDataObject() { object dataObject = this.AssociatedObject; if (!string.IsNullOrEmpty(this.PropertyPath)) { dataObject = null; Type t = this.AssociatedObject.GetType(); PropertyInfo propertyInfo = t.GetProperty(this.PropertyPath, BindingFlags.Public | BindingFlags.Instance); if (propertyInfo != null) { dataObject = propertyInfo.GetValue(this.AssociatedObject, null); } } return (dataObject); } static IEnumerable<DropTrigger> GetDropTriggersOfFirstElement(Point p) { var elements = VisualTreeHelper.FindElementsInHostCoordinates(p, Application.Current.RootVisual); if (elements != null) { foreach (var item in elements) { var triggers = Expression.Interaction.GetTriggers(item); if ((triggers != null) && (triggers.Count > 0)) { foreach (Expression.TriggerBase trigger in triggers) { if (trigger is DropTrigger) { yield return (DropTrigger)trigger; } } break; } } } } void OnMouseDown(object sender, MouseButtonEventArgs e) { if (!isDragging) { isDragging = true; this.AssociatedObject.CaptureMouse(); } } bool isDragging; DragDropUIInjector injectedUI; Point capturePoint; } }