Having Trouble Setting Window's Owner in Parent's Constructor

后端 未结 4 1347
慢半拍i
慢半拍i 2021-02-12 14:25

Is there anything wrong in WPF with setting the Owner property of a window to its parent in that parent\'s constructor? There shouldn\'t be, right? So why am I getting an

4条回答
  •  被撕碎了的回忆
    2021-02-12 14:46

    The problem is that because WPF only creates the native window the first time a WPF Window is shown, you can't be setting a not-yet-shown Window as an Owner (since that establishes a native window "owner -> owned" relationship, but the native handle doesn't yet exist.)

    You can handle the StateChanged event on the owner window, ensure that the new state is "shown", and then set the owned window's Owner at that point. Alternatively, you could create and show the owned window at that point.

提交回复
热议问题