How to repaint a WinFormsHost when it becomes visible?
I've a winforms control within a WindowsFormsHost on a WPF control. 开发者_JAVA百科The WPF control is only visible some of the time, and when it becomes visible the contents of the winforms control have usually changed.
When the ViewModel for the WPF control changes I change the contents of the winforms control and the WPF control becomes visible.
Unfortunately, the previous contents of the winforms control is repainted, as if from a visual cache. I've run it through the debugger and I know that the winforms control is having its data updated, but it won't repaint until I re-size the program window (when a repaint is clearly triggered).
I've tried Invalidate() on the winforms control and InvalidateVisual(), InvalidateArrange() and InvalidateMeasure() on the WPF control inside the DataContextChanged event handler for the WPF control, but it seems that because the WPF control is not visible at this point (it's just about to become visible) these method calls are swallowed.
Anyone got any clever ideas on how to force a repaint of a WinFormsHost-ed winforms control immediately after the hosting WPF control becomes visible?
Did you try to invalidate the WinForms control in the IsVisibleChanged
event handler ?
You should be able to call Refresh() on the hosted Control as soon as you make it visible. Refresh()
, as per the documentation:
Forces the control to invalidate its client area and immediately redraw itself and any child controls.
This turned out to be due to the underlying data-structure failing to notify that it had changed - nothing to do with the paint methods at all! :( Thanks for your help guys.
精彩评论