1

私のitemscontrol:

 <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を抽出する方法です。

2つ目は、すべてのデータ項目を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;
            }                    
   }

したがって、明確にするために、コレクションをトラバースして、データテンプレートを介して割り当てられた基になるタイプを抽出することはしません。

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 に答える