在WPF中通过其Uid获取对象 [英] Get object by its Uid in WPF

查看:227
本文介绍了在WPF中通过其Uid获取对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF中有一个控件,该控件具有唯一的Uid.如何通过其Uid检索对象?

I have an control in WPF which has an unique Uid. How can I retrive the object by its Uid?

推荐答案

您几乎必须通过蛮力来做到这一点.这是您可以使用的辅助程序扩展方法:

You pretty much have to do it by brute-force. Here's a helper extension method you can use:

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;
}

然后您可以这样称呼它:

Then you can call it like this:

var el = FindUid("someUid");

这篇关于在WPF中通过其Uid获取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆