my 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
第二件事是将所有数据项遍历为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分配的基础类型。
答案 0 :(得分:1)
您可以通过调用以下代码来获取椭圆:
// here i would need the ellipse that the object represents
var container = control.ItemContainerGenerator.ContainerFromItem(item);
var ellipse = VisualTreeHelper.GetChild(container, 0);