如何找到 UserControl 父级?
How to Find UserControl Parent?
我想从标记中找到用户控件的父级,
我使用了以下代码
var hostRoot = serviceProvider.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider;
var host = hostRoot.RootObject;
var do = host as DependencyObject;
var uc = do as UserControl;
var ucParent = uc.Parent;
和
var wp = Window.GetWindow(uc);
但是return都为null
更新:
<Window x:Class="WpfApp12.MainWindow">
<Grid>
<local:UserControl1/>
</Grid>
</Window>
和
<UserControl x:Class="WpfApp12.UserControl1">
<StackPanel>
<TextBlock Text="{me:myMarkup}"/>
</StackPanel>
</UserControl>
更新 2:
这是我的标记
public class myMarkup : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
var hostRoot = serviceProvider.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider;
var host = hostRoot.RootObject;
var xv = host as DependencyObject;
var ww = Window.GetWindow(xv);
return null;
}
}
更新 3:
示例项目
https://github.com/ghost1372/HandyControls/files/6259944/WpfApp18.zip
您需要等到 UserControl
加载完毕再调用 Window.GetWindow
:
public class myMarkup : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
var hostRoot = serviceProvider.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider;
var host = hostRoot.RootObject;
var xv = host as FrameworkElement;
//local function:
void OnLoaded(object sender, RoutedEventArgs e)
{
FrameworkElement xv = (FrameworkElement)sender;
Window parentWindow = Window.GetWindow(xv);
//do something with the window...
}
Window parentWindow;
if (xv.IsLoaded)
{
parentWindow = Window.GetWindow(xv);
//do something with the window...
}
else
{
xv.Loaded += OnLoaded;
}
return null;
}
}
我想从标记中找到用户控件的父级, 我使用了以下代码
var hostRoot = serviceProvider.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider;
var host = hostRoot.RootObject;
var do = host as DependencyObject;
var uc = do as UserControl;
var ucParent = uc.Parent;
和
var wp = Window.GetWindow(uc);
但是return都为null
更新:
<Window x:Class="WpfApp12.MainWindow">
<Grid>
<local:UserControl1/>
</Grid>
</Window>
和
<UserControl x:Class="WpfApp12.UserControl1">
<StackPanel>
<TextBlock Text="{me:myMarkup}"/>
</StackPanel>
</UserControl>
更新 2:
这是我的标记
public class myMarkup : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
var hostRoot = serviceProvider.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider;
var host = hostRoot.RootObject;
var xv = host as DependencyObject;
var ww = Window.GetWindow(xv);
return null;
}
}
更新 3: 示例项目
https://github.com/ghost1372/HandyControls/files/6259944/WpfApp18.zip
您需要等到 UserControl
加载完毕再调用 Window.GetWindow
:
public class myMarkup : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
var hostRoot = serviceProvider.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider;
var host = hostRoot.RootObject;
var xv = host as FrameworkElement;
//local function:
void OnLoaded(object sender, RoutedEventArgs e)
{
FrameworkElement xv = (FrameworkElement)sender;
Window parentWindow = Window.GetWindow(xv);
//do something with the window...
}
Window parentWindow;
if (xv.IsLoaded)
{
parentWindow = Window.GetWindow(xv);
//do something with the window...
}
else
{
xv.Loaded += OnLoaded;
}
return null;
}
}