如何从窗口获取所有按钮? (将IsEnabled属性设置为false)Windows Phone 7:如何从窗口获取所有按钮?
0
A
回答
2
您可以遍历所有控件。例如:
foreach (var ctrl in LayoutRoot.Children)
{
if (ctrl is Button)
((Button)ctrl).IsEnabled = false;
}
当然,LayoutRoot
是默认的名称。如果需要,您可以将其更改为另一个容器。
编辑为允许递归嵌套面板(在评论中提到)。
private void DisableAllButtons(Panel parent)
{
foreach (var ctrl in parent.Children)
{
if (ctrl is Button)
{
((Button)(ctrl)).IsEnabled = false;
}
else
{
if (ctrl is Panel)
{
if (((Panel)ctrl).Children.Count > 0)
{
DisableAllButtons((Panel)ctrl);
}
}
}
}
}
1
那么,DisableAllButtons()有时可能会工作,但通常是不够的。这是一个真实世界的例子。 (经过一些简化。)
ListBox ScrollViewer Border Grid ScrollContentPresenter ItemsPresenter VirtualizingStackPanel ListBoxItem ContentPresenter Grid TextBlock TextBlock Button ListBoxItem ContentPresenter Grid TextBlock TextBlock TextBlock ScrollBar Grid Grid RepeatButton Thumb Rectangle RepeatButton
如果你想要一个可靠的解决方案,然后代替枚举面板小孩使用 VisualTreeHelper类及其方法GetCildrenCount()和GetChild()。这是代码:
void DisableAllButtons(FrameworkElement fe)
{
if (fe is Button)
((Button)(fe)).IsEnabled = false;
int count = VisualTreeHelper.GetChildrenCount(fe);
for (int index = 0; index < count; ++index)
{
DisableAllButtons((FrameworkElement)VisualTreeHelper.GetChild(fe, index));
}
}
相关问题
- 1. Windows Phone 7按钮
- 2. Windows Phone 7弹出窗口
- 3. 按钮颜色-Windows Phone 7
- 4. Windows Phone 7按钮状态
- 5. Windows phone 7按钮“边框”
- 6. 如何在windows phone 7上从vb.net打开浏览器窗口?
- 7. 在Windows Phone 7中获取UI按钮的事件
- 8. 从Windows Phone 8发送信息到PC窗口7窗体
- 9. 如何获取所有窗口组?
- 10. Windows:如何获取所有可见窗口的列表?
- 11. Windows Phone 7开发延迟按钮
- 12. Windows Phone 7后退按钮问题
- 13. 更改按钮文本 - Windows Phone 7
- 14. Windows Phone 7上的TextWrapping按钮
- 15. 在Windows Phone 7中的按钮命令
- 16. 带图像的Windows Phone 7按钮
- 17. silverlight windows phone 7中的按钮问题
- 18. Windows Phone 7物理按钮超载
- 19. Windows Phone 7的搜索按钮
- 20. Windows Phone 7中的透明按钮
- 21. longlistselector中的windows phone 7按钮
- 22. Windows Phone 7中的按钮阵列
- 23. 获取所有chrome窗口
- 24. 如何获取有关窗口的按钮位置
- 25. 如何在按下开始按钮(Windows Phone)时按下“开始”按钮(Windows Phone)
- 26. 如何从mp4容器获取aac音频? (Windows Phone 7和C#)
- 27. 如何在Windows Phone 7中从Facebook获取提要?
- 28. 如何从我的Windows Phone 7仿真器获取数据
- 29. 如何 - Windows Phone 7?
- 30. 如何获取所有联系人并写入Windows Phone 7文件
这不需要递归地完成?例如,如果'LayoutRoot'包含另一个包含'Button'的Panel。 – Praetorian 2011-05-31 23:12:05
@Praetorian:我假定用户只是指一个容器,但是你是对的,如果该容器包含另一个容器,那么它将需要递归地完成。 @Tony - 你有嵌套的容器吗? – keyboardP 2011-05-31 23:15:46
编辑答案只是有嵌套面板。 – keyboardP 2011-05-31 23:23:22