Reputation: 1388
I have an application where need to dynamically create and move textblocks around in a canvas later save the layout and the load the layout. Problem i am facing is tiring to get the position of the textblock. I tried this two method, but its not working
item.GetValue(TranslateTransform.XProperty).ToString();//always give zero
Canvas.GetTop(item);//always gives the initial position, does not update after dragging.
Upvotes: 0
Views: 105
Reputation: 5083
Getting coords from controls:
foreach (UIElement el in mapGrid.Children)
{
XElement control = new XElement("control");
var ele = (HumanWorkspace)el;
Vector v = VisualTreeHelper.GetOffset(el);
double x = v.X;
double y = v.Y;
XAttribute atd = new XAttribute("direction", ele.Direction.ToString("d"));
XAttribute atx = new XAttribute("x", v.X.ToString());
XAttribute aty = new XAttribute("y", v.Y.ToString());
control.Add(atd);
control.Add(atx);
control.Add(aty);
controls.Add(control);
}
Setting coords when loading state:
foreach (XElement ele in doc.Elements("controls"))
{
var con = new HumanWorkspace();
con.Direction = (WorkspaceDirection)int.Parse(ele.Attribute("direction").Value);
con.SetValue(TranslateTransform.XProperty, double.Parse(ele.Attribute("x").Value));
con.SetValue(TranslateTransform.YProperty, double.Parse(ele.Attribute("y").Value));
}
Upvotes: 1