在 C# WPF 中将文本环绕图像或链接两个 TextBlock

发布于 2024-09-11 15:59:54 字数 214 浏览 5 评论 0原文

我正在创建一个在同一窗口中显示文本和图像的程序。图像位于屏幕的左上角,文本将从图像的右侧开始,然后继续向下延伸到图像下方。

目前,我正在尝试获取两个 TextBlock(一个位于图像右侧,一个位于图像和第一个文本块下方),并希望文本从一个块继续到另一个块。这是一种理想的方法吗?如果是的话,我会怎么做?有没有比这更好/更简单的方法,或者我可以只用一个对象来做到这一点?谢谢,

安德鲁

I am creating a program that shows text and an image in the same window. The Image is in the top-left corner of the screen and the Text will begin to the right of it, and then continue down below the image.

Currently, what I am trying is to take two TextBlocks (one to the right of the image and one below both the image and the first textblock) and want to have the text continue from one block to the other. Is this an ideal approach, and if so, how would I do it? Is there a better/easier way than this or can I do it with just one object? Thanks,

Andrew

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

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

发布评论

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

评论(2

扭转时空 2024-09-18 15:59:54

我不建议使用 TextBlocks 来实现这种类型的布局。正如 Kieren 所建议的,FlowDocument 对于此类设计来说是理想的选择。使用带有 Paragraph 元素和包含图像的 Floater 元素的 FlowDocument 查看此 XAML 代码段和生成的 WPF 应用程序屏幕截图:

<Grid>
    <FlowDocumentScrollViewer>
        <FlowDocument>                
            <Paragraph>
                <Floater Width="130" HorizontalAlignment="Left" Margin="0,0,5,5" Padding="3">
                    <BlockUIContainer>
                        <Image Source="/FlowDocumentTest;component/dog.png" Width="100" /> 
                    </BlockUIContainer>
                </Floater>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
                Suspendisse et diam felis. Vestibulum ac nisl mi. 
                Etiam varius velit lobortis nibh vestibulum nec consequat velit pellentesque. 
                Cras commodo libero placerat nulla dapibus eget porttitor ligula tempor. 
                Donec nisl massa, congue et pretium sit amet, feugiat vel est. 
                Nulla dapibus metus in justo pulvinar sit amet viverra lorem rhoncus. 
                Integer placerat interdum massa et mattis.</Paragraph>
        </FlowDocument>
    </FlowDocumentScrollViewer>
</Grid>

I would not recommend using TextBlocks to achieve this type of layout. As Kieren suggests, a FlowDocument would be ideal for this type of a design. Take a look at this XAML snippet and the resulting WPF app screenshot using a FlowDocument with a Paragraph element and a Floater element containing an image:

<Grid>
    <FlowDocumentScrollViewer>
        <FlowDocument>                
            <Paragraph>
                <Floater Width="130" HorizontalAlignment="Left" Margin="0,0,5,5" Padding="3">
                    <BlockUIContainer>
                        <Image Source="/FlowDocumentTest;component/dog.png" Width="100" /> 
                    </BlockUIContainer>
                </Floater>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
                Suspendisse et diam felis. Vestibulum ac nisl mi. 
                Etiam varius velit lobortis nibh vestibulum nec consequat velit pellentesque. 
                Cras commodo libero placerat nulla dapibus eget porttitor ligula tempor. 
                Donec nisl massa, congue et pretium sit amet, feugiat vel est. 
                Nulla dapibus metus in justo pulvinar sit amet viverra lorem rhoncus. 
                Integer placerat interdum massa et mattis.</Paragraph>
        </FlowDocument>
    </FlowDocumentScrollViewer>
</Grid>

alt text

温柔少女心 2024-09-18 15:59:54

在后面的代码中尝试这个:

            FlowDocument fd = new FlowDocument();

            Paragraph p = new Paragraph();
            System.Windows.Controls.Image image = new System.Windows.Controls.Image();
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = GetUri("Images/ScanHead.png", typeof(PrintService));
            bi.EndInit();
            image.Stretch = Stretch.None;
            image.Source = bi;

            InlineUIContainer b = new InlineUIContainer(image);
            b.BaselineAlignment = BaselineAlignment.Center;
            p.Inlines.Add(b);
            InlineUIContainer b2 = new InlineUIContainer(new TextBlock() { Text="Text\r\n2nd row"});
            p.Inlines.Add(b2);

            p.FontStyle = FontStyles.Normal;
            p.FontFamily = new FontFamily("Arial");
            p.FontSize = 18;
            fd.Blocks.Add(p);

In code behind try this:

            FlowDocument fd = new FlowDocument();

            Paragraph p = new Paragraph();
            System.Windows.Controls.Image image = new System.Windows.Controls.Image();
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = GetUri("Images/ScanHead.png", typeof(PrintService));
            bi.EndInit();
            image.Stretch = Stretch.None;
            image.Source = bi;

            InlineUIContainer b = new InlineUIContainer(image);
            b.BaselineAlignment = BaselineAlignment.Center;
            p.Inlines.Add(b);
            InlineUIContainer b2 = new InlineUIContainer(new TextBlock() { Text="Text\r\n2nd row"});
            p.Inlines.Add(b2);

            p.FontStyle = FontStyles.Normal;
            p.FontFamily = new FontFamily("Arial");
            p.FontSize = 18;
            fd.Blocks.Add(p);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文