WPF User Control Parent

后端 未结 17 1084
不知归路
不知归路 2020-11-28 02:18

I have a user control that I load into a MainWindow at runtime. I cannot get a handle on the containing window from the UserControl.

I hav

17条回答
  •  隐瞒了意图╮
    2020-11-28 02:33

    I've found that the parent of a UserControl is always null in the constructor, but in any event handlers the parent is set correctly. I guess it must have something to do with the way the control tree is loaded. So to get around this you can just get the parent in the controls Loaded event.

    For an example checkout this question WPF User Control's DataContext is Null

提交回复
热议问题