Focus border outside an ElementHost is not being displayed

579 Views Asked by At

I'm using an ElementHost to host a WPF custom control in a WinForms app. The control has a thick border which should be visible when the control has focus. The border is "outside" the control, i.e, has a negative margin.

The problem is that the focus border never shows up. If I add a border with padding containing my control to the ElementHost, it gets visible. However, I'd like to have the control inside the ElementHost as it is, so it doesn't break a layout which was made before using a stub. Any idea?

Thanks.

1

There are 1 best solutions below

1
On BEST ANSWER

The ElementHost has its own window handle, which hosts all of the WPF elements inside it. It's a separate WinForms control.

Generally speaking, a window handle can't draw outside its bounding rectangle (which, in this case, would be the bounding rectangle of the ElementHost, relative to its parent WinForms control).

So no, you can't use a negative margin on your WPF element to draw outside the ElementHost. You'll need to either draw the border in WinForms instead, or modify your layout so that the ElementHost is larger.