WPFNewbie
WPFNewbie

Reputation: 2534

Make a control topmost in WPF

I have a form where the user can select various form elements and drag them around. Since the user can customize the layout of the form I am using a canvas and all of the elements are children of it.

When two elements are in the same region whichever one was added last as a child of the canvas will be drawn on top. I want to be able to make whatever the active element, the one being dragged, to be set to be the topmost element.

I played around with the SetZOrder method, setting it to 0, but it does nothing. Upon inspection all of my elements calling GetZOrder I get a 0 for each of them, which is why setting it to 0 doesn't make a difference.

The only solution that I have found, and it does work, is to remove and add the element to the canvas.

        _mainCanvas.Children.Remove(_selectedElement);
        _mainCanvas.Children.Add(_selectedElement);

While this works, I feel there must be a more prefered and proper way of doing this.

Upvotes: 1

Views: 3776

Answers (1)

Chris E
Chris E

Reputation: 983

The ZIndex does determine which is top most. The highest ZIndex wins. Try setting ZIndex of the one you want to be topmost to a number higher than the ZIndex of the others.

Upvotes: 2

Related Questions