[英]WPF + Controls.UserControl. How to find a control inside?
In my WPF project, I have a System.Windows.Controls.UserControl control.在我的 WPF 项目中,我有一个 System.Windows.Controls.UserControl 控件。 How to find a control inside that contol?
如何在该控件中找到控件?
use VisualTree, if I understood your question correctly.如果我正确理解了您的问题,请使用 VisualTree。
refer to msdn: http://msdn.microsoft.com/en-us/library/dd409789.aspx参考msdn: http://msdn.microsoft.com/en-us/library/dd409789.aspx
In that case you would probably want to walk the visual tree, like this extension method does:在这种情况下,您可能想要遍历可视化树,就像这个扩展方法一样:
internal static T FindVisualChild<T>(this DependencyObject parent) where T : DependencyObject
{
if (parent == null)
{
return null;
}
DependencyObject parentObject = parent;
int childCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childCount; i++)
{
DependencyObject childObject = VisualTreeHelper.GetChild(parentObject, i);
if (childObject == null)
{
continue;
}
var child = childObject as T;
return child ?? FindVisualChild<T>(childObject);
}
return null;
}
It requires that you know the type of the control you are looking for.它要求您知道要查找的控件的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.