我有一个应用程序需要在画布中动态创建和移动文本块,稍后保存布局并加载布局。我面临的问题是获取文本块的位置很累。我尝试了这两种方法,但它不起作用
item.GetValue(TranslateTransform.XProperty).ToString();//always give zero
Canvas.GetTop(item);//always gives the initial position, does not update after dragging.
我有一个应用程序需要在画布中动态创建和移动文本块,稍后保存布局并加载布局。我面临的问题是获取文本块的位置很累。我尝试了这两种方法,但它不起作用
item.GetValue(TranslateTransform.XProperty).ToString();//always give zero
Canvas.GetTop(item);//always gives the initial position, does not update after dragging.
从控件获取坐标:
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);
}
加载状态时设置坐标:
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));
}