1

我的项目控制:

 <ItemsControl x:Name="MyItemsControl"  Style="{StaticResource ItemsControlStyle}" />

 <Style TargetType="{x:Type ItemsControl}" x:Key="ItemsControlStyle">
      <Setter Property="ItemTemplate" Value="{StaticResource ItemsControlDataItem}"></Setter>
 </Style>

 <DataTemplate x:Key="ItemsControlDataItem" >
      <Ellipse Width="45" Height="45"></Ellipse>
 </DataTemplate>

iv'e 挂钩了一个事件以查看基础集合何时更改:

 ((INotifyCollectionChanged)MyItemsControl.Items).CollectionChanged += new NotifyCollectionChangedEventHandler(ClientWindow_CollectionChanged);

我需要的第一件事是提取拥有此 ItemCollection 的 ItemsControl

第二件事是将所有数据项作为它们的 DataTemplate 遍历,即作为 Ellipse,因为我不想对它们执行一些转换。

   void ClientWindow_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
   {
        // here i need to traverse and make my change , how do i extract the ellipse items  
        // how do i get the itemsControl associated with the ItemCollection which triggered this event                
            ItemCollection collection = sender as ItemCollection ;
            foreach (object item in collection)
            {
                //  here i would need the ellipse that the object represents 
                // EDIT : i'm guessing this is how i would get the ellipse    
                // but how would i get the itemsControl ?
                var ellipse = _itemsControl.ItemContainerGenerator.ContainerFromItem(item ) as Ellipse;
            }                    
   }

所以只是为了澄清我不想遍历集合并提取通过 datatemplate 分配的基础类型。

4

1 回答 1

1

您可以通过调用以下代码来获取椭圆:

//  here i would need the ellipse that the object represents 
var container = control.ItemContainerGenerator.ContainerFromItem(item);
var ellipse = VisualTreeHelper.GetChild(container, 0);
于 2012-03-22T20:25:29.340 回答