BringToFront在WPFBringToFront、WPF

2023-09-04 00:38:10 作者:冇厼、懑哫ろ

我需要带至前在WPF自定义控件。

I need to bring to front a custom control in WPF.

伪code

OnMouseDown()
{
    if (this.parent != null)
        this.parent.BringToFront(this);
}

我知道,我知道的,有一个 zIndex的,但还是不明白如何更换简单的WinForm BringToFront parent.SetZIndex(这一点,?MaxZIndex(母)+ 1)

I know, I know that there are a ZIndex, but still don't understand how to replace the simple WinForm BringToFront to parent.SetZIndex(this, ?MaxZIndex(parent)? + 1) ?

或许有做像WPF的这样一个很酷的事情的一个更好的办法?!

Perhaps there is a better way of doing it in the such a cool thing like WPF?!..

推荐答案

真的,你可以期望的最好的是让一个元素最上面的相对于相同父面板中的任何同级元素(如StackPanel中,网格,帆布等)。在面板的z顺序的项目是由Panel.ZIndex附加属性。

Really the best that you can hope for is to make an element top-most with respect to any sibling elements in the same parent Panel (such as StackPanel, Grid, Canvas, etc). The z-order of the items in the panels is controlled by the Panel.ZIndex attached property.

例如:

<Grid>
    <Rectangle Fill="Blue" Width="50" Height="50" Panel.ZIndex="1" />
    <Rectangle Fill="Red" Width="50" Height="50" Panel.ZIndex="0" />
</Grid>

在这个例子中,蓝色矩形将出现在顶部。这说明了一点在这个博客文章。

In this example, the blue rectangle would appear on top. This is explained a bit more in this blog post.

此外,还有一个问题,改变Panel.ZIndex没有立即反映在用户界面中。有一个私人的方法,使刷新z索引,但因为它是私人它不是使用它是一个好主意。若要绕它,你必须删除并重新添加了孩子。

There is also an issue with changes to the Panel.ZIndex not being immediately reflected in the UI. There is a private method that allows refreshes the z-index, but since it's private it's not a good idea to use it. To work-around it, you'd have to remove and re-add the children.

您却无法做出任何给定的元素最顶层。例如:

You cannot however make any given element top-most. For example:

<Grid>
    <Grid>
        <Rectangle Fill="Blue" Width="50" Height="50" Panel.ZIndex="1" />
        <Rectangle Fill="Red" Width="50" Height="50" Panel.ZIndex="0" />
    </Grid>
    <Grid>
        <Rectangle Fill="Green" Width="50" Height="50" Panel.ZIndex="0" />
        <Rectangle Fill="Yellow" Width="50" Height="50" Panel.ZIndex="0" />
    </Grid>
</Grid>

在这种情况下,黄色矩形将被显示。因为第二内格示出在第一内网格的顶部和所有它的内容。你必须要改变它,像这样把蓝色矩形顶端:

In this case, the yellow rectangle will be displayed. Because the second inner grid is shown on top of the first inner grid and all it's content. You'd have to alter it like so to bring the blue rectangle to the top:

<Grid>
    <Grid Panel.ZIndex="1">
        <Rectangle Fill="Blue" Width="50" Height="50" Panel.ZIndex="1" />
        <Rectangle Fill="Red" Width="50" Height="50" Panel.ZIndex="0" />
    </Grid>
    <Grid Panel.ZIndex="0">
        <Rectangle Fill="Green" Width="50" Height="50" Panel.ZIndex="0" />
        <Rectangle Fill="Yellow" Width="50" Height="50" Panel.ZIndex="0" />
    </Grid>
</Grid>

当有一个ItemsControl,this问题是相关的。大多数其他控件有一个孩子,但你仍然需要给他们带来期待作出任何的是孩子们最顶端。

When dealing with an ItemsControl, this question is relevant. Most other controls have a single child, but you'd still need to bring them forward to make any of it's children top-most.

最后,装饰器的是把事情放在第一位的好方法,但他们不是主要的视觉树的一部分。所以,我不知道,将工作你的情况。

Finally, Adorners are a good way to put things on top of everything, but they are not part of the main visual tree. So I'm not sure that would work in your case.

 
精彩推荐
图片推荐