在Silverlight / WPF带来元素向前(Z指数) [英] Bring element forward (Z Index) in Silverlight/WPF

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

问题描述

所有的文档和例子我发现在网上设置Z-指数在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容器内边界元素。我使用的MouseEnter和鼠标离开事件,以便他们长大徘徊时触发的ScaleTransform.ScaleX动画和ScaleTransform.ScaleY。因为它们可以改变大小并占据相同的空间中的容器(s)时,最近添加的项都重叠旧项目(相对于当前调整项)的其他项目。有没有干净的方式把目前的项目向前code,我触发我的动画,让他们重叠的所有其他项目时,他们正在调整?

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?

推荐答案

我不得不处理这事。

假设你有一个设置为自定义控制的实例一个ItemTemplate一个ItemsControl。内控制你做Canvas.SetZIndex(这一点,99)。它不会工作,因为这不是的ItemsControl的ItemsPanel的直接孩子。 ItemsControl的创建每个项目的内容presenter,即下降到ItemsPanel和内容presenter内呈现的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的,你必须要找到它的内容presenter,并更改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天全站免登陆