wpf - WPF中通过Uid获取对象

标签 wpf object wpf-controls fetch

我在 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/

相关文章:

c# - 如何通过更改文本来更改组合框中的下拉项?

c# - WPF 中的音量控制元素

wpf - 在 WPF 中使用单独的事件/禁用图像创建工具栏按钮的最佳方法是什么?

wpf - 在 3D 对象 wpf 中放置一个控件

wpf - 如何在 WPF 中自由裁剪图像?

c# - 为什么 Equals 和 ReferenceEquals 方法的结果不同,即使变量是引用类型?

java - 每次程序运行时创建一个新的但不同的对象

javascript - 如何获取 javascript 对象属性的子集

c# - 没有集合的 TreeView 递归

c# - 在 WPF Datagrid 中将动态图像设置为自动生成的列