Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF equivalent to Silverlight "RootVisual"

I am trying to port an application from silverlight to wpf. Unfortunatley I am new to both. Is there an equvivalent to the following Silverlight code in WPF?

        private static Canvas GetCanvas()
        {
            var uc = Application.Current.RootVisual as UserControl;
            if (uc == null)
            {
                return null;
            }
            return uc.FindName("ChoiceCanvas") as Canvas;
        }

Currently I am using

Application.Current.MainWindow.FindName("ChoiceCanvas") as Canvas;

But this doesn't work, perhaps because ChoiceCanvas is something located in a UserControl and not in the MainWindow?

like image 578
tobsen Avatar asked Feb 07 '10 13:02

tobsen


2 Answers

There is no RootVisual property in WPF. As far as I understand, the "Window" is the "root". You can get the Window that any WPF (D.O.) object belongs to by running the static method Window myWindow = Window.GetWindow(myControl);

like image 188
Jeremiah Morrill Avatar answered Oct 20 '22 19:10

Jeremiah Morrill


FindName won't work becuase the Canvas exists in the namescope of the UserControl, try using the LogicalTreeHelper instead.

 var canvas = LogicalTreeHelper.FindLogicalNode(
      Application.Current.MainWindow, "ChoiceCanvas") as Canvas; 
like image 37
Ian Oakes Avatar answered Oct 20 '22 18:10

Ian Oakes