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

后端 未结 4 1358
慢半拍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 15:04

    You need the WPF equivalent of HandleCreated event, which is SourceInitialized. This should work:

    public OwnerWindow()
    {
        InitializeComponent();
    
        SourceInitialized += (s, a) =>
            {
                var owned = new OwnedWindow();
                owned.Owner = this;
            };
    }
    

    Note that you don't have to Show either OwnerWindow or OwnedWindow for this to work.

提交回复
热议问题