How do I bring an item to the front in wpf?

前端 未结 4 446
闹比i
闹比i 2021-01-17 09:43

I simply have two grid on top of one another. Given one state of the world, I want grid A to be on top, given another state of the world, I want grid B to be on top. In the

4条回答
  •  花落未央
    2021-01-17 10:14

    To whom it may concern:

    ZIndex property is 0 by default, so if you have (like me) a Canvas with more than 1 element (>4000 Shapes in my case), all will have ZIndex = 0, so changing the ZIndexes with this method will have no effect.

    For this to work, I set the ZIndexes to a known value after creating all the elements, so they can be ordered after.

        int zIndex = 0; 
        for (int i = 0; i < canvas.Children.Count; i++) {
            UIElement child = canvas.Children[i] as UIElement;
            if (canvas.Children[i] is UIElement) Canvas.SetZIndex(child, zIndex++);
        }
    

提交回复
热议问题