我在 WPF 中有一个控件,它有一个唯一的 Uid。如何通过 Uid 检索对象?
最佳答案
你几乎必须通过暴力来做到这一点。这是您可以使用的辅助扩展方法:
private static UIElement FindUid(this DependencyObject parent, string uid)
{
var count = VisualTreeHelper.GetChildrenCount(parent);
if (count == 0) return null;
for (int i = 0; i < count; i++)
{
var el = VisualTreeHelper.GetChild(parent, i) as UIElement;
if (el == null) continue;
if (el.Uid == uid) return el;
el = el.FindUid(uid);
if (el != null) return el;
}
return null;
}
然后你可以这样调用它:
var el = FindUid("someUid");
关于wpf - WPF中通过Uid获取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1887611/