c# vs 2008 inkcanvas SelectionChanging
My application displays shapes on an inkcanvas that can be selected moved and
resized using event SelectionChanging="OnSelectionChanging"
this canvas is saved to an .xaml file
selectionchanging event works perfect on the on new inkcanvas, but when
canvas is saved and opened and displayed on screen the selection moves the
shapes to xy co-ordinates 0,0 (locationInInkCanvas )
Point locationInInkCanvas = element.TranslatePoint(new Point(0, 0),
myinkcanvas);
public void OnSelectionChanging(object sender,
InkCanvasSelectionChangingEventArgs e)
{
on_change_inkcanvas( sender, e);
}
public void on_change_inkcanvas(object sender,
InkCanvasSelectionChangingEventArgs e)
{
ReadOnlyCollection<UIElement> elements = e.GetSelectedElements();
foreach (UIElement element in elements)
{
// obtain actual location of element relative to InkCanvas
Point locationInInkCanvas = element.TranslatePoint(new
Point(0, 0), myinkcanvas);
element.SetValue(InkCanvas.LeftProperty,
locationInInkCanvas.X);
element.SetValue(InkCanvas.TopProperty,
locationInInkCanvas.Y);
element.SetValue(TextBlock.ForegroundProperty, new
SolidColorBrush(col));
// un-set right/bottom properties
// element.SetValue(InkCanvas.RightProperty, double.NaN);
// element.SetValue(InkCanvas.BottomProperty, double.NaN);
// re-translate any render transform
Matrix matRender = element.RenderTransform.Value;
matRender.Translate(-matRender.OffsetX, -matRender.OffsetY);
element.RenderTransform = new MatrixTransform(matRender);
// set margins to zero
if (element is FrameworkElement)
{
((FrameworkElement)element).Margin = new Thickness(0d);
}
}
}
public struct User_Var
Save canvas to .xaml file
xamlFile = new FileStream(canvas_fname, FileMode.Create,
FileAccess.ReadWrite);
currentFileName = canvas_fname;
XamlWriter.Save(this.myinkcanvas, xamlFile);
Open Xaml file on to myinkcanvas
xamlFile = new FileStream(thecanvasname, FileMode.Open,
FileAccess.ReadWrite);
XmlReader xmlReader = XmlReader.Create(xamlFile);
InkCanvas myinkcanvas =
(InkCanvas)XamlReader.Load(xmlReader);
canvas_file_open = true;
myinkcanvas.SelectionChanging += new
InkCanvasSelectionChangingEventHandler(on_change_inkcanvas);
WordScroll.Content = myinkcanvas;
date: Thu, 3 Apr 2008 14:33:01 -0700
author: amitch