在 Silverlight/WPF 中前移元素(Z 索引) [英] Bring element forward (Z Index) in Silverlight/WPF

查看:25
本文介绍了在 Silverlight/WPF 中前移元素(Z 索引)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上找到的所有用于设置 Z-Index 以在 Silverlight 中引入元素的文档和示例都使用 Canvas 元素作为容器.

All the documentation and examples I'm finding online for setting Z-Index to bring an element forward in Silverlight are using a Canvas element as a container.

我的项目是 DataTemplate 中 ItemsControl 容器内的 Border 元素.我正在使用 MouseEnter 和 MouseLeave 事件来触发 ScaleTransform.ScaleX 和 ScaleTransform.ScaleY 上的动画,以便它们在悬停时增长.由于它们被调整大小并与容器中的其他项目占据相同的空间,最近添加的项目与旧项目重叠(与当前调整大小的项目相反).是否有一种干净的方法可以在我触发动画的代码中将当前项目向前推进,以便它们在调整大小时与所有其他项目重叠?

My items are Border elements inside of an ItemsControl container in a DataTemplate. I'm using the MouseEnter and MouseLeave events to trigger an animation on the ScaleTransform.ScaleX and ScaleTransform.ScaleY so they grow when hovered. As they're resized and occupying the same space as other items in the container(s), the most recently added items are overlapping the older items (as opposed to the currently resizing item). Is there a CLEAN way to bring the current item forward in code where I trigger my animation so that they overlap all other items when they're resized?

推荐答案

我不得不处理这个问题.

I've had to deal with this.

假设您有一个 ItemsControl,其中 ItemTemplate 设置为自定义控件的实例.在该控件中,您可以执行 Canvas.SetZIndex(this, 99).它不起作用,因为this"不是 ItemsControl 的 ItemsPanel 的直接子项.ItemsControl 为每个项目创建一个 ContentPresenter,将其放入 ItemsPanel,并在 ContentPresenter 中呈现 ItemTemplate.

Say you have an ItemsControl with an ItemTemplate set to an instance of a custom control. Within that control you do Canvas.SetZIndex(this, 99). It won't work, because "this" is not the immediate child of the ItemsControl's ItemsPanel. The ItemsControl creates a ContentPresenter for each item, drops that into the ItemsPanel, and renders the ItemTemplate within the ContentPresenter.

因此,如果您想更改控件中的 ZIndex,您必须找到它的 ContentPresenter,并在其上更改 ZIndex.一种方法是...

So, if you want to change the ZIndex within your control, you have to find its ContentPresenter, and change the ZIndex on that. One way is...

        public static T FindVisualParent<T>( this DependencyObject obj )
        where T : DependencyObject
    {
        DependencyObject parent = VisualTreeHelper.GetParent( obj );
        while ( parent != null )
        {
            T typed = parent as T;
            if ( typed != null )
            {
                return typed;
            }
            parent = VisualTreeHelper.GetParent( parent );
        }
        return null;
    }
                ContentPresenter foo = this.FindVisualParent<ContentPresenter>();
            Canvas.SetZIndex( foo, 99 );

这篇关于在 Silverlight/WPF 中前移元素(Z 索引)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆