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?
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);
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With