How can I add multiple FlowDocumentReaders to a StackPanel?

为君一笑 提交于 2019-12-10 15:33:45

问题


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

    }
}

回答1:


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


来源:https://stackoverflow.com/questions/2288161/how-can-i-add-multiple-flowdocumentreaders-to-a-stackpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!