如何将多个 FlowDocumentReader 添加到 StackPanel?

发布于 2024-08-22 04:56:27 字数 2303 浏览 9 评论 0原文

感谢Leom的回答我是能够通过将 FlowDocument 包装在 FlowDocumentReader 中来将其添加到 StackPanel

但现在我有两个问题:

  • 似乎只添加了第一个 FlowDocumentReader,其余的被忽略,
  • 有一个不需要的边距,我无法摆脱

如何我可以向 StackPanel 添加多个 FlowDocumentReader 吗?

alt text http:// /www.deviantsart.com/upload/1ndiqqe.png

XAML:

<Window x:Class="TestFlowdoc23432.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="200" Width="300">
    <StackPanel Margin="10">
        <ContentControl x:Name="MainArea"/>
    </StackPanel>
</Window>

代码隐藏:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace TestFlowdoc23432
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            StackPanel sp = new StackPanel();

            TextBlock tb1 = new TextBlock();
            tb1.Text = "first text block";
            sp.Children.Add(tb1);

            TextBlock tb2 = new TextBlock();
            tb2.Text = "second text block";
            sp.Children.Add(tb2);

            sp.Children.Add(GetFlowDocumentReader("first flow document reader"));
            sp.Children.Add(GetFlowDocumentReader("second flow document reader"));

            MainArea.Content = sp;
        }

        FlowDocumentReader GetFlowDocumentReader(string text)
        {
            FlowDocumentReader fdr = new FlowDocumentReader();
            FlowDocument fd = new FlowDocument();
            fdr.Document = fd;
            fdr.Margin = new Thickness(0);
            Paragraph par = new Paragraph();
            par.Margin = new Thickness(0);
            fd.Blocks.Add(par);

            Run r = new Run(text);
            par.Inlines.Add(r);

            return fdr;
        }

    }
}

Thanks to Leom's answer I was able to add a FlowDocument to a StackPanel by wrapping it in a FlowDocumentReader.

But now I have two problems:

  • it seems only the first FlowDocumentReader is added and the rest ignored
  • there is an unwanted margin that I can't get rid of

How can I add multiple FlowDocumentReaders to a StackPanel without the unwanted margin?

alt text http://www.deviantsart.com/upload/1ndiqqe.png

XAML:

<Window x:Class="TestFlowdoc23432.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="200" Width="300">
    <StackPanel Margin="10">
        <ContentControl x:Name="MainArea"/>
    </StackPanel>
</Window>

Code Behind:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace TestFlowdoc23432
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            StackPanel sp = new StackPanel();

            TextBlock tb1 = new TextBlock();
            tb1.Text = "first text block";
            sp.Children.Add(tb1);

            TextBlock tb2 = new TextBlock();
            tb2.Text = "second text block";
            sp.Children.Add(tb2);

            sp.Children.Add(GetFlowDocumentReader("first flow document reader"));
            sp.Children.Add(GetFlowDocumentReader("second flow document reader"));

            MainArea.Content = sp;
        }

        FlowDocumentReader GetFlowDocumentReader(string text)
        {
            FlowDocumentReader fdr = new FlowDocumentReader();
            FlowDocument fd = new FlowDocument();
            fdr.Document = fd;
            fdr.Margin = new Thickness(0);
            Paragraph par = new Paragraph();
            par.Margin = new Thickness(0);
            fd.Blocks.Add(par);

            Run r = new Run(text);
            par.Inlines.Add(r);

            return fdr;
        }

    }
}

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

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

发布评论

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

评论(1

风苍溪 2024-08-29 04:56:27

要使文本显示在左侧,您需要在流程文档上设置 pagepadding 属性,如下所示:

fd.PagePadding = new Thickness(0);

您似乎只获得第一个读者的原因是因为它填充了可用空间(将其移动到第一个对象,您将看不到文本块)。
如果将 FlowDocumentReader 更改为 FlowDocumentScrollViewer 并使用 VerticalScrollBarVisibility 属性,则可以获得所需的效果。以下是应用了更改的 GetFlowDocumentReader 方法:

FlowDocumentScrollViewer GetFlowDocumentReader(string text)
        {
            FlowDocumentScrollViewer fdr = new FlowDocumentScrollViewer();

            FlowDocument fd = new FlowDocument();
            fdr.Document = fd;
            fdr.Margin = new Thickness(0);
            Paragraph par = new Paragraph();
            par.Margin = new Thickness(0);
            fd.Blocks.Add(par);

            Run r = new Run(text);
            par.Inlines.Add(r);

            fd.PagePadding = new Thickness(0);
            fdr.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;

            return fdr;
        }

To make the text appear to the left you need to set the pagepadding property on your flowdocument as follows:

fd.PagePadding = new Thickness(0);

the reason that you only seem to get the first reader is becuase it fills the space available (move it to be the first object and you will not see the textblocks).
If you change the FlowDocumentReader to be a FlowDocumentScrollViewer and use the VerticalScrollBarVisibility property then you can get the desired effect. The following is your GetFlowDocumentReader method with the changes applied:

FlowDocumentScrollViewer GetFlowDocumentReader(string text)
        {
            FlowDocumentScrollViewer fdr = new FlowDocumentScrollViewer();

            FlowDocument fd = new FlowDocument();
            fdr.Document = fd;
            fdr.Margin = new Thickness(0);
            Paragraph par = new Paragraph();
            par.Margin = new Thickness(0);
            fd.Blocks.Add(par);

            Run r = new Run(text);
            par.Inlines.Add(r);

            fd.PagePadding = new Thickness(0);
            fdr.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;

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