在 Silverlight 中删除 UIElement 的父级

发布于 2024-10-26 23:09:56 字数 825 浏览 2 评论 0原文

我有一个 UIElement 对象,如何删除它的父对象?

我发现 UIElement 的父属性没有设置器。

任何建议都会有帮助。

编辑 :

 protected FrameworkElement Content
        {
            get { return this.content; }
            set
            {
                if ((this.content != null) && (this.Children.Contains(this.content)
== true))
                    this.Children.Remove(this.content);
                this.content = value;
                if ((this.content != null) && (this.Children.Contains(this.content)
== false))
                {
                    this.Children.Add(this.content); // here i get error Element is already an child of another
                }

                this.InvalidateMeasure();
                this.InvalidateArrange();
            }
        }

I have an object of an UIElement, how can I remove the parent of it?

I saw that there is not setter for parent property of an UIElement.

Any suggestions will be helpful.

EDIT :

 protected FrameworkElement Content
        {
            get { return this.content; }
            set
            {
                if ((this.content != null) && (this.Children.Contains(this.content)
== true))
                    this.Children.Remove(this.content);
                this.content = value;
                if ((this.content != null) && (this.Children.Contains(this.content)
== false))
                {
                    this.Children.Add(this.content); // here i get error Element is already an child of another
                }

                this.InvalidateMeasure();
                this.InvalidateArrange();
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老子叫无熙 2024-11-02 23:09:56

UIElement.Parent 只是将父级返回为一个 UIElement。如果您知道父元素是什么,则可以将其强制转换为正确的元素。假设您有一个父级,这是一个 StackPanel

 StackPanel parent = myelement.Parent as StackPanel;
 parent.Children.Remove(myelement);//removes your element from its parent.

UIElement.Parent just returns the Parent as a UIElement. You can cast it to the right element if you know what the parent is. Lets say you have a parent this IS a StackPanel

 StackPanel parent = myelement.Parent as StackPanel;
 parent.Children.Remove(myelement);//removes your element from its parent.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文