检查 Silverlight 中是否加载了控件
我正在尝试编写一个扩展方法,使我能够将焦点集中在控件上。我已经编写了下面的方法,该方法工作正常,但是如果控件已经加载,那么显然连接 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果控件尚未加载,则其模板内的各种元素将不会被构造。使用 Linq-to-VisualTree您可以确认这一点:
第一个调试输出应显示“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:
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.
或者检查父级就足够了:
如果父级为空,则未加载控件(因为它在可视化树中没有父级)
or it is enough to check the parent:
if the parent is null, then the control is not loaded (because it doesn't have a parent in a visual tree)