c# - 如何迭代 LibraryStack 中的元素?

标签 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="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>

现在在代码隐藏中,我想遍历 LibraryStack 中包含的所有标签(数据模板中定义的标签)。

但是如果我用

foreach (FrameworkElement element in TaggingContainer.Items) {
}

我迭代了 TaggingContainer 中的数据对象,而不是数据模板中的数据对象。我该如何改变它?

最佳答案

最简单的方法可能是在 Visual Tree 中的 LibraryStack 下找到每个 LibraryStackItemLabel。试试这个

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

或者,您可以获取每个元素的容器,并以这种方式获取 Label

foreach (var element in TaggingContainer.Items)
{
    LibraryStackItem libraryStackItem = TaggingContainer.ItemContainerGenerator.ContainerFromItem(element) as LibraryStackItem;
    Label label = VisualTreeHelpers.GetVisualChild<Label>(libraryStackItem);
}

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;
}

获取VisualChildCollection

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);
        }
    }
}

关于c# - 如何迭代 LibraryStack 中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5079918/

相关文章:

c# - 为什么可以在同一个类中创建的另一个线程中访问局部变量?

c# - 我应该如何正确连接两个表,以便我可以访问其中一个实体中的属性?

c# - 搜索具有任何属性值的页面

c# - 无法解析样式、ResourceDictionary、UserControl

c# - 如何在进一步处理之前检查列表中的任何空值?

javascript - 对 JSON 数据进行编码以保留 json 格式

c# - 处理 URL 和获取数据

wpf - 文字渲染仍然模糊 : Best font for WPF application?

自定义文本框的 WPF 验证错误模板

c# - SMTP 发送获取 SmtpFailedRecipientException