获取包含 Block 对象的 BlockCollection 对象

发布于 2024-09-29 05:06:35 字数 243 浏览 10 评论 0原文

获取包含给定块类型对象的 BlockCollection 对象(System.Windows.Documents 命名空间)的最简单(可能也更快)的方法是什么?

我找不到使用 Block 类上可用的任何直接方法来执行此操作。

public static BlockCollection FindContainingCollection(Block block)
{
   // ???        
}

What would be the easiet (and possibly fast) way to get a BlockCollection object (System.Windows.Documents namespace) which contains a given Block-type object?

I could not find any direct way of doing this using what's available on Block class.

public static BlockCollection FindContainingCollection(Block block)
{
   // ???        
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我一直都在从未离去 2024-10-06 05:06:35

因为我发布这篇文章已经一个月了,但没有得到答复,我将发布我最终做了什么。我这样写是相当愚蠢的,但我找不到任何更干净的方法来实现它,主要是因为 Blocks 属性不是通过公共接口公开的。

    public BlockCollection FindContainingCollection(Block block)
    {
        BlockCollection blocks = null;

        FlowDocument flowDoc = block.Parent as FlowDocument;

        if (flowDoc != null)
            blocks = flowDoc.Blocks;
        else
        {
            TableCell tableCell = block.Parent as TableCell;
            if (tableCell != null)
                blocks = tableCell.Blocks;
            else
            {
                ListItem listItem = block.Parent as ListItem;
                if (listItem != null)
                    blocks = listItem.Blocks;
                else
                {
                    Section section = block.Parent as Section;
                    if (section != null)
                        blocks = section.Blocks;
                    else
                    {
                        Figure figure = block.Parent as Figure;
                        if (figure != null)
                            blocks = figure.Blocks;
                        else
                        {
                            Floater floater = block.Parent as Floater;
                            if (floater != null)
                                blocks = floater.Blocks;
                        }
                    }
                }
            }
        }

        return blocks;
    }

Because It's been a month since I posted this and got no answer, I'll post what I ended up doing. It's rather silly that I wrote this way, but I could not figure out any cleaner way to implement it, mainly because Blocks property is not exposed through a common interface.

    public BlockCollection FindContainingCollection(Block block)
    {
        BlockCollection blocks = null;

        FlowDocument flowDoc = block.Parent as FlowDocument;

        if (flowDoc != null)
            blocks = flowDoc.Blocks;
        else
        {
            TableCell tableCell = block.Parent as TableCell;
            if (tableCell != null)
                blocks = tableCell.Blocks;
            else
            {
                ListItem listItem = block.Parent as ListItem;
                if (listItem != null)
                    blocks = listItem.Blocks;
                else
                {
                    Section section = block.Parent as Section;
                    if (section != null)
                        blocks = section.Blocks;
                    else
                    {
                        Figure figure = block.Parent as Figure;
                        if (figure != null)
                            blocks = figure.Blocks;
                        else
                        {
                            Floater floater = block.Parent as Floater;
                            if (floater != null)
                                blocks = floater.Blocks;
                        }
                    }
                }
            }
        }

        return blocks;
    }
病女 2024-10-06 05:06:35

使用 ElementAt(int32)

for(int i = 0; i < blocks.Count; i++) {
    Block block = blocks.ElementAt(i);
    Console.WriteLine("{0}", block.GetType());
}

Use ElementAt(int32)

for(int i = 0; i < blocks.Count; i++) {
    Block block = blocks.ElementAt(i);
    Console.WriteLine("{0}", block.GetType());
}
给我一枪 2024-10-06 05:06:35

Block 有一个属性 SiblingBlocks 可以满足您的需要。

BlockCollection containingCollection = block.SiblingBlocks

https:// msdn.microsoft.com/en-us/library/system.windows.documents.block.siblingblocks(v=vs.100).aspx

Block has a property SiblingBlocks which does what you need.

BlockCollection containingCollection = block.SiblingBlocks

https://msdn.microsoft.com/en-us/library/system.windows.documents.block.siblingblocks(v=vs.100).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文