将一个集合的数组分别绑定到多个itemscontrols

时间:2012-03-06 21:41:20

标签: wpf data-binding

我有一系列的收藏品,每一个都被限制在一个物品控制中 以下列方式:

     ItemsContropl_n.ItemsSource= Collection[n-1] ;   
    .
    ItemsControl_15.ItemsSource= Collection[14] ;
    ItemsControl_14.ItemsSource= Collection[13] ; 
    .
    .

有没有办法可以为每个控件定义相对于它的集合对应项的项源 以下内容:

itemscontrols祖先的DataContext有一个公开集合数组的属性

  public Class Board 
  {
      public Coll [] Collections 
      {
         get { return collections;}
      }
  }


  CollectionPanel.DataContext = Board ;

  <ItemsControl x:Name="ItemsControl_i" itemsSource="{Binding Path=Coll[i-1]" />

2 个答案:

答案 0 :(得分:3)

是的,但您必须使用另一位父ItemsControl ...

   <ItemsControl DataContext={Binding MyBoardInstance}
                 ItemsSource={Binding Collections}>
        <ItemsControl.ItemTemplate>
           <DataTemplate>   
             <ItemsControl ItemsSource={Binding}>
                 <ItemsControl.ItemTemplate>
                       <DataTemplate>
                           <!-- Your Template goes here -->
                       </DataTemplate>
                 </ItemsControl.ItemTemplate>
             </ItemsControl>
           </DataTemplate>
        </ItemsControl.ItemTemplate>
   </ItemsControl>

希望这会有所帮助......

答案 1 :(得分:0)

没有。它会简化构建ItemsControls列表的事情,然后通过迭代ItemsControls和集合来代码中进行绑定吗?如果它是一个网格样式板,它可能有助于将集合中的控件放在哪里。

for (int i=0; i<my_controls; i++)
    my_controls[i].ItemsSource = Collection[i];