检查 Silverlight 中是否加载了控件

发布于 2024-11-07 09:11:59 字数 1669 浏览 3 评论 0原文

我正在尝试编写一个扩展方法,使我能够将焦点集中在控件上。我已经编写了下面的方法,该方法工作正常,但是如果控件已经加载,那么显然连接 Loaded 事件不会有任何用处 - 我还需要一种方法来检查是否控件已加载,因此我可以简单地运行 Focus() 代码,而无需连接事件。

有没有办法在控件上模拟 IsLoaded 属性?

public static void SetFocus(this Control control)
{
    // return if the control is not visible
    if (control.Visibility == Visibility.Collapsed) { return; }

    control.Loaded += (sender, routedeventArgs) =>
        {
            // focus the Silverlight plugin first
            System.Windows.Browser.HtmlPage.Plugin.Focus();

            control.IsTabStop = true; // required to allow Focus
            control.Focus();

            if (control is TextBox)
            {
                ((TextBox)control).SelectAll();
            }
        };
}

编辑:根据下面 ColinE 的回答,我是这样实现的:

public static void SetFocus(this Control control)
{
    // return if the control is not visible
    if (control.Visibility == Visibility.Collapsed) { return; }

    if (control.Descendants().Count() > 0)
    {
        // already loaded, just set focus and return
        SetFocusDelegate(control);
        return;
    }

    // not loaded, wait for load before setting focus
    control.Loaded += (sender, routedeventArgs) =>
    {
        SetFocusDelegate(control);
    };
}

public static void SetFocusDelegate(Control control)
{
    // focus the Silverlight plugin first
    System.Windows.Browser.HtmlPage.Plugin.Focus();

    control.IsTabStop = true; // required to allow Focus
    control.Focus();

    if (control is TextBox)
    {
        ((TextBox)control).SelectAll();
    }
}

I'm trying to write an extension method that will allow me to set focus on a Control. I've written the method below which works fine, however if the control is already loaded then obviously hooking up the Loaded event isn't going to be any use - I also need a way to check if the control has been loaded so I can simply run the Focus() code without hooking up the event.

Is there any way to simulate an IsLoaded property on a control?

public static void SetFocus(this Control control)
{
    // return if the control is not visible
    if (control.Visibility == Visibility.Collapsed) { return; }

    control.Loaded += (sender, routedeventArgs) =>
        {
            // focus the Silverlight plugin first
            System.Windows.Browser.HtmlPage.Plugin.Focus();

            control.IsTabStop = true; // required to allow Focus
            control.Focus();

            if (control is TextBox)
            {
                ((TextBox)control).SelectAll();
            }
        };
}

EDIT: As per ColinE's answer below, I implemented it like this:

public static void SetFocus(this Control control)
{
    // return if the control is not visible
    if (control.Visibility == Visibility.Collapsed) { return; }

    if (control.Descendants().Count() > 0)
    {
        // already loaded, just set focus and return
        SetFocusDelegate(control);
        return;
    }

    // not loaded, wait for load before setting focus
    control.Loaded += (sender, routedeventArgs) =>
    {
        SetFocusDelegate(control);
    };
}

public static void SetFocusDelegate(Control control)
{
    // focus the Silverlight plugin first
    System.Windows.Browser.HtmlPage.Plugin.Focus();

    control.IsTabStop = true; // required to allow Focus
    control.Focus();

    if (control is TextBox)
    {
        ((TextBox)control).SelectAll();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深已缘浅 2024-11-14 09:11:59

如果控件尚未加载,则其模板内的各种元素将不会被构造。使用 Linq-to-VisualTree您可以确认这一点:

Debug.WriteLine(control.Descendants().Count());

control.Loaded += (s, e) =>
  {
    Debug.WriteLine(foo.Descendants().Count());
  };

第一个调试输出应显示“0”,第二个调试输出将是一个数字>0,表示应用模板后控件的子元素数。

If a control has not been loaded, then the various elements within its template will not have been constructed. Using Linq-to-VisualTree you can confirm this:

Debug.WriteLine(control.Descendants().Count());

control.Loaded += (s, e) =>
  {
    Debug.WriteLine(foo.Descendants().Count());
  };

The first debug output should show '0', the second will be a number >0 which indicates the number of child elements of the control once the template has been applied.

執念 2024-11-14 09:11:59

或者检查父级就足够了:

var parent = System.Windows.Media.VisualTreeHelper.GetParent(control);

如果父级为空,则未加载控件(因为它在可视化树中没有父级)

or it is enough to check the parent:

var parent = System.Windows.Media.VisualTreeHelper.GetParent(control);

if the parent is null, then the control is not loaded (because it doesn't have a parent in a visual tree)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文