WPF User Control Parent

后端 未结 17 1135
不知归路
不知归路 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:43

    It's working for me:

    DependencyObject GetTopLevelControl(DependencyObject control)
    {
        DependencyObject tmp = control;
        DependencyObject parent = null;
        while((tmp = VisualTreeHelper.GetParent(tmp)) != null)
        {
            parent = tmp;
        }
        return parent;
    }
    

提交回复
热议问题