【问题标题】:Best way to store multiple controls in an array将多个控件存储在数组中的最佳方法
【发布时间】:2009-02-16 02:26:25
【问题描述】:

我在 WPF 中有一个带有许多子控件的 UserControl,我想像数组一样对其进行索引。这些子控件与我不感兴趣的其他子控件位于同一网格控件中。

我希望能够以类似于以下方式索引这些控件:

someControl.Children[3];

不必避免我不感兴趣的控件。这是我所拥有的示例:

<Grid x:Name="gCalendar">
    // more crap here...

    <TextBlock Grid.Row="0" Grid.Column="7" TextBlock.TextAlignment="Center">Blah</TextBlock>


    <Internal:DayCalendarTime Grid.Row="1" Grid.Column="0" />
    <Internal:DayCalendarCore Grid.Row="1" Grid.Column="1"/>
    <Internal:DayCalendarCore Grid.Row="1" Grid.Column="2"/>
    <Internal:DayCalendarCore Grid.Row="1" Grid.Column="3"/>
    <Internal:DayCalendarCore Grid.Row="1" Grid.Column="4" />
    <Internal:DayCalendarCore Grid.Row="1" Grid.Column="5"/>
    <Internal:DayCalendarCore Grid.Row="1" Grid.Column="6"/>
    <Internal:DayCalendarCore Grid.Row="1" Grid.Column="7"/>
</Grid>

我想要一个仅由 Internal:DayTimeCore 控件组成的数组,方法是在它们周围放置某种包装控件。

这可能吗,还是我必须通过循环遍历网格的所有子节点并添加我感兴趣的类型来手动创建数组?

【问题讨论】:

    标签: wpf controls


    【解决方案1】:

    另一种非 WPF 方式, 通过

    创建控件集合背后的代码
    foreach(DayCalendarTime control in gCalendar.Children)
    { 
      controlCollection[i++] = control;
    }
    

    【讨论】:

      【解决方案2】:

      您可以将这些控件包装在另一个 Grid 控件中

      【讨论】:

      • 没有比这更轻的了吗?
      猜你喜欢
      • 2021-06-19
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 2014-05-02
      • 2010-09-28
      • 2012-08-15
      • 2010-11-11
      相关资源
      最近更新 更多