有没有办法获取UIElementCollection中的项

本文关键字:UIElementCollection 获取 有没有 | 更新日期: 2023-09-27 18:05:18

我在网格中有一堆复选框。我想访问它们,这样我就可以根据启动时的注册表值将它们设置为选中或未选中

我已经得到了网格的孩子作为一个UIElementCollection,并过滤掉其他类型。但是,似乎没有任何方法可以访问存储在集合中的数据。有简单的方法吗?

有没有办法获取UIElementCollection中的项

UIElementCollectionIEnumerable,所以你可以在它上面循环。

foreach( UIElement child in myUIElementCollection)
{
}

为了简化按类型或其他方式搜索控件,您可以使用VisualTreeHelper并以这种方式遍历元素:

foreach (UIElement childElement in myUIElementCollection) 
{
  for (int i = 0; i < VisualTreeHelper.GetChildrenCount(childElement); i++)
  {
     DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
     var checkBox = child as CheckBox;
     if (checkBox != null)
     {
         // update it
     }
  }
}

BTW,为什么不像IsRegistryValueFound那样暴露布尔属性,只是在XAML中将其绑定到CheckBox.IsChecked属性?

相关文章:
  • 没有找到相关文章