Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal sealed partial class MainWindow
- {
- //Image is an object of type System.Windows.Controls.Image from XAML: <Image x:Name="Image" ...
- //Canvas is an object of type System.Windows.Controls.Canvas from XAML: <Canvas x:Name="Canvas" ...
- private IDisposable _unsubscribe;
- public MainWindow()
- {
- InitializeComponent();
- var mouseDownEvents = from e in Observable.FromEventPattern<MouseButtonEventHandler, MouseEventArgs>
- (
- listener => listener.Invoke,
- listener => Image.MouseDown += listener,
- listener => Image.MouseDown -= listener
- )
- select e.EventArgs.GetPosition(Image);
- var mouseMoveEvents = from e in Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>
- (
- listener => listener.Invoke,
- listener => Image.MouseMove += listener,
- listener => Image.MouseMove -= listener
- )
- select e.EventArgs.GetPosition(Canvas);
- var mouseUpEvents = Observable.FromEventPattern<MouseButtonEventHandler, MouseEventArgs>
- (
- listener => listener.Invoke,
- listener => Image.MouseUp += listener,
- listener => Image.MouseUp -= listener
- );
- var mouseLeaveEvents = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>
- (
- listener => listener.Invoke,
- listener => Image.MouseLeave += listener,
- listener => Image.MouseLeave -= listener
- );
- var dragAndDrops =
- from clickPosition in mouseDownEvents
- from movePosition in mouseMoveEvents.TakeUntil(mouseUpEvents.Merge(mouseLeaveEvents))
- select new
- {
- X = movePosition.X - clickPosition.X,
- Y = movePosition.Y - clickPosition.Y
- };
- _unsubscribe = dragAndDrops.ObserveOn(SynchronizationContext.Current).Subscribe(newImagePosition =>
- {
- Canvas.SetLeft(Image, newImagePosition.X);
- Canvas.SetTop(Image, newImagePosition.Y);
- });
- }
- private void StopTracking(object sender, RoutedEventArgs e)
- {
- if (null != _unsubscribe)
- {
- _unsubscribe.Dispose();
- _unsubscribe = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement