C# 如何迭代LibraryStack中的元素?

C# 如何迭代LibraryStack中的元素?,c#,.net,wpf,pixelsense,C#,.net,Wpf,Pixelsense,我对LibraryStack有以下定义: <s:LibraryStack Name="TaggingContainer" Margin="20" Grid.Row="1" Grid.ColumnSpan="2" AllowDrop="True" Height="300" Width="300" s:SurfaceDragDrop.DragLeave="TaggingContainer_DragLeave" s:SurfaceDragDrop.DragEnter="TaggingContai

我对LibraryStack有以下定义:

<s:LibraryStack Name="TaggingContainer" Margin="20" Grid.Row="1" Grid.ColumnSpan="2" AllowDrop="True" Height="300" Width="300" s:SurfaceDragDrop.DragLeave="TaggingContainer_DragLeave" s:SurfaceDragDrop.DragEnter="TaggingContainer_DragEnter" s:SurfaceDragDrop.PreviewDrop="LibraryStack_PreviewDrop">
            <s:LibraryStack.ItemTemplate>
                <DataTemplate>
                    <Label Content="{Binding Name}" Tag="{Binding}" FontSize="20" Margin="10,10,10,10" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" Background="#c49b14" BorderBrush="White" BorderThickness="2" s:Contacts.PreviewContactDown="Label_PreviewContactDown"></Label>
                </DataTemplate>
            </s:LibraryStack.ItemTemplate>
        </s:LibraryStack>

我迭代TaggingContainer中的数据对象,而不是Datatemplates。如何更改此设置?

最简单的方法可能是在可视化树中的
LibraryStack
下找到每个
LibraryStackItem
标签。试试这个

private void SomeMethod()
{
    // Get All Labels
    List<Label> labels = GetVisualChildCollection<Label>(TaggingContainer);
    foreach (Label label in labels)
    {
        //...
    }
}
GetVisualChild

private static T GetVisualChild<T>(DependencyObject parent) where T : Visual
{
    T child = default(T);

    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T;
        if (child == null)
        {
            child = GetVisualChild<T>(v);
        }
        if (child != null)
        {
            break;
        }
    }
    return child;
}
private static T GetVisualChild(DependencyObject父对象),其中T:Visual
{
T child=默认值(T);
int numVisuals=VisualTreeHelper.GetChildrenCount(父级);
对于(int i=0;i
GetVisualChildCollection

public static List<T> GetVisualChildCollection<T>(object parent) where T : Visual
{
    List<T> visualCollection = new List<T>();
    GetVisualChildCollection(parent as DependencyObject, visualCollection);
    return visualCollection;
}
private static void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : Visual
{
    int count = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < count; i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(parent, i);
        if (child is T)
        {
            visualCollection.Add(child as T);
        }
        else if (child != null)
        {
            GetVisualChildCollection(child, visualCollection);
        }
    }
}
public static List GetVisualChildCollection(对象父对象),其中T:Visual
{
List visualCollection=新列表();
GetVisualChildCollection(父对象作为DependencyObject,visualCollection);
返回视觉采集;
}
私有静态void GetVisualChildCollection(DependencyObject父对象,列出visualCollection),其中T:Visual
{
int count=VisualTreeHelper.GetChildrenCount(父级);
for(int i=0;i
应该注意,这种方法适用于从ItemsControl派生的任何控件(listbox、combobox、treeview等)。这里的表面没什么特别的。。。
private static T GetVisualChild<T>(DependencyObject parent) where T : Visual
{
    T child = default(T);

    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T;
        if (child == null)
        {
            child = GetVisualChild<T>(v);
        }
        if (child != null)
        {
            break;
        }
    }
    return child;
}
public static List<T> GetVisualChildCollection<T>(object parent) where T : Visual
{
    List<T> visualCollection = new List<T>();
    GetVisualChildCollection(parent as DependencyObject, visualCollection);
    return visualCollection;
}
private static void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : Visual
{
    int count = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < count; i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(parent, i);
        if (child is T)
        {
            visualCollection.Add(child as T);
        }
        else if (child != null)
        {
            GetVisualChildCollection(child, visualCollection);
        }
    }
}