在它被绑定之后迭代ItemsControl的itemssource

时间:2015-05-17 08:52:53

标签: c# xaml windows-phone

鉴于我上一个问题的反馈,我决定将其删除,并再次详细描述我想要完成的事情:

旧问题:

  

我已经为ItemControl尝试了很多事件处理程序,但却发现每次我尝试在其事件中迭代itemControl时,它还没有受到它的itemssource的约束。

     

有没有办法在ItemControl的ItemsSource绑定后触发事件?

     

它必须特定于ItemsControl而不是例如的ListView。

我想做的是迭代通过itemssource绑定的ItemsControl项目。我希望这样做一次,并且必须在它的ItemsSource属性通过ViewModel绑定之后的某个时刻。

这可以通过使用ListView而不是使用它的SelectionChanged事件轻松完成,但我希望使用ItemsControl,因为我在ListView上使用VisualTreeHelper时遇到问题。

我的代码的一个简单示例:

<ItemsControl x:Name="BoundToObservableCollection" ItemsSource="{Binding ElementName=CalendarDateItemName, Path=CalendarItemsShow}">
    <ItemsControl.ItemTemplate>
    <DataTemplate>
        <StackPanel x:Name="CatchThisControl"/>
    </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

C#:

// I wish to execute this after the ItemsSoucr of my ItemsControl has been bound.
ItemsControl list = BoundToObservableCollection;
foreach (var item in list.Items)
{
    var _Container = list.ItemContainerGenerator.ContainerFromItem(item);
    var _Children = AllChildren(_Container);
}

public List<UIElement> AllChildren(DependencyObject parent)
{
    var _List = new List<UIElement> { };
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
    {
        var _Child = VisualTreeHelper.GetChild(parent, i);
        if (_Child is UIElement)
            _List.Add(_Child as UIElement); 
        _List.AddRange(AllChildren(_Child));
    }
    return _List;
}

在ItemsControl的所有事件中,itemssource尚未绑定。 AllChildren受到约束后如何解雇?

0 个答案:

没有答案