Why does overriding the WPF [removed] method fail to draw to the screen?

后端 未结 4 1788
面向向阳花
面向向阳花 2021-02-08 00:02

I noticed that when overriding the System.Windows.Window OnRender method fails to draw to the screen. I\'m sure there\'s a good reason for this but was unable to find the explan

相关标签:
4条回答
  • 2021-02-08 00:16

    I think, that Grid generate this problem. Becouse by default Window contains Grid element

    0 讨论(0)
  • 2021-02-08 00:17

    The answer is given by Chales Petzold here.

    His answer is 'don't know why', however a comment correctly suggests that setting the window's Background to transparent fixes the bug.

    0 讨论(0)
  • 2021-02-08 00:17

    just as a check, does the snippet work with the dc.Draw... lines removed (i.e. is it the act of overriding or the additional draw commands that cause the issue)?

    I found a link that suggests that simply re-ordering the calls may solve the issue (i.e. doing the dc.Draw... calls before calling base.OnRender(dc)).

    http://www.codeproject.com/Messages/3356653/Override-OnRender-class-inherits-frameworkelement.aspx

    0 讨论(0)
  • 2021-02-08 00:30

    Actually, the Window paints itself, which among others is a rectangle for the background. Whatever is added in the OnRender is behind the background. Make the Background transparent and one can see what is behind it.

    At least that is what I found out when using Button and OnRender. If I draw only within the button, nothing can be seen, because the button is drawn over it. But if the drawing extends beyond the button boundaries, anything outside can be seen.

    enter image description here

    Here an empty button is drawn over a yellow background. In the OnRender, I draw 2 black lines forming a cross. But only the parts of the line outside the button can be seen.

    Very strange behavior of OnRender, indeed !

    0 讨论(0)
提交回复
热议问题