1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfBringIntoViewCanvas
{
/// <summary>
/// Logique d'interaction pour WinItemsCanvasMaitreDetail.xaml
/// </summary>
public partial class WinItemsCanvasMaitreDetail : Window
{
public WinItemsCanvasMaitreDetail()
{
InitializeComponent();
}
private void Canvas_Map_Loaded(object sender, RoutedEventArgs e)
{
}
//"panning": MouseDown,MouseMove et MouseUp
// nb:cette methode est valable sur tout controle scrollable.....
private Point pointDown = new Point(-1, -1);
private Point pointMove = new Point(-1, -1);
private Point currentOffset = new Point(-1, -1);
private bool isDown = false;
private void Canvas_Map_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
pointDown = Mouse.GetPosition((ListBox)sender);
if (pointDown != new Point(-1, -1))
{
isDown = true ;
((ListBox)sender).CaptureMouse();
}
}
private void Canvas_Map_MouseMove(object sender, MouseEventArgs e)
{
pointMove = Mouse.GetPosition((ListBox)sender);
if (isDown)
{
Vector dragOffset = pointMove - pointDown;
this.currentOffset.X -= dragOffset.X;
this.currentOffset.Y -= dragOffset.Y;
sv1.ScrollToHorizontalOffset(this.currentOffset.X);
sv1.ScrollToVerticalOffset(this.currentOffset.Y);
}
}
private void Canvas_Map_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
pointDown = new Point(-1, -1);
currentOffset = new Point(-1, -1);
isDown = false;
((ListBox)sender).ReleaseMouseCapture();
}
private void Canvas_Map_MouseWheel(object sender, MouseWheelEventArgs e)
{
}
private void Canvas_Map_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
// deplacer la fenetre "viewport" du scrollviewer
// vers le point souhaite
private void lbSymbolDataItem_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int ndx = this.lbSymbolDataItem.SelectedIndex;
if (ndx != -1)
{
Node nd = (Node)this.lbSymbolDataItem.Items[ndx];
sv1.ScrollToHorizontalOffset(nd.X);
sv1.ScrollToVerticalOffset(nd.Y);
// on peut aussi utiliser aussi ce bout de code qui amene
// deplace la fenetre "viewport" du scrollviewer sur l'item ......
// ListBoxItem lbItemCanvas = (ListBoxItem)this.Canvas_Map.ItemContainerGenerator.ContainerFromIndex(ndx);
// if (lbItemCanvas != null)
// {
// lbItemCanvas.BringIntoView();
// }
}
}
// deplacer la fenetre "viewport" du scrollviewer
// vers le centre du canvas
private void btnToCentreCanvas_Click(object sender, RoutedEventArgs e)
{
sv1.ScrollToHorizontalOffset(this.Canvas_Map.Width / 2- this.sv1.ViewportWidth/2);
sv1.ScrollToVerticalOffset(this.Canvas_Map.Height / 2 - this.sv1.ViewportHeight / 2);
}
}
} |
Partager