0

後でキャンバス内でテキストブロックを動的に作成して移動し、後でレイアウトを保存してレイアウトをロードする必要があるアプリケーションがあります。私が直面している問題は、テキストブロックの位置を取得するのに疲れています。この2つの方法を試しましたが、機能しません

item.GetValue(TranslateTransform.XProperty).ToString();//always give zero
Canvas.GetTop(item);//always gives the initial position, does not update after dragging.
4

1 に答える 1

1

コントロールから座標を取得する:

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));
            }
于 2012-09-09T05:38:25.687 に答える