怎样才能多FlowDocumentReaders添加到Stack

怎样才能多FlowDocumentReaders添加到Stack

本文介绍了我怎样才能多FlowDocumentReaders添加到StackPanel的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢 Leom的答案我能够通过在 FlowDocumentReader


$ b $包裹它的的FlowDocument 添加到的StackPanel b

但现在我有两个问题:




  • 似乎只有第一个添加FlowDocumentReader和休息忽略

  • 有一个不需要保证金,我无法摆脱


$的b $ b

如何,我可以多FlowDocumentReaders添加到StackPanel中没有多余的保证金





XAML:

 <窗口x:类=TestFlowdoc23432.Window1
的xmlns =http://schemas.microsoft.com/winfx/2006 / XAML /演示
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=窗口1HEIGHT =200WIDTH =300> ;
< StackPanel的保证金=10>
< ContentControl中X:NAME =MAINAREA/>
< / StackPanel的>
< /窗GT;



代码背后:



 使用System.Windows;使用System.Windows.Controls的
;使用System.Windows.Documents
;

命名空间TestFlowdoc23432
{
公共部分类窗口1:窗口
{
公共窗口1()
{
的InitializeComponent() ;

StackPanel的SP =新的StackPanel();

TextBlock的TB1 =新的TextBlock();
tb1.Text =第一个文本块;
sp.Children.Add(TB1);

TextBlock的TB2 =新的TextBlock();
tb2.Text =第二个文本块;
sp.Children.Add(TB2);

sp.Children.Add(GetFlowDocumentReader(第一流的文档阅读器));
sp.Children.Add(GetFlowDocumentReader(第二流的文档阅读器));

MainArea.Content = SP;
}

FlowDocumentReader GetFlowDocumentReader(字符串文本)
{
FlowDocumentReader FDR =新FlowDocumentReader();
的FlowDocument FD =新的FlowDocument();
fdr.Document = FD;
fdr.Margin =新厚度(0);
款面值=新的第();
par.Margin =新厚度(0);
fd.Blocks.Add(PAR);

运行R =新的运行(文本);
par.Inlines.Add(R);

返回FDR;
}

}
}


解决方案

要使文本显示你需要设置你的FlowDocument的pagepadding财产左侧如下:

  fd.PagePadding =新的厚度(0); 

这是你似乎只得到第一个读者是监守它填补可用空间(移动它的原因成为第一个对象,你不会看到的TextBlocks)。
。如果你改变了FlowDocumentReader是一个FlowDocumentScrollViewer并使用VerticalScrollBarVisibility属性,那么你可以得到预期的效果。以下是你GetFlowDocumentReader方法应用了变化:

  FlowDocumentScrollViewer GetFlowDocumentReader(字符串文本)
{
FlowDocumentScrollViewer FDR =新FlowDocumentScrollViewer();

的FlowDocument FD =新的FlowDocument();
fdr.Document = FD;
fdr.Margin =新厚度(0);
款面值=新的第();
par.Margin =新厚度(0);
fd.Blocks.Add(PAR);

运行R =新的运行(文本);
par.Inlines.Add(R);

fd.PagePadding =新厚度(0);
fdr.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;

返回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?

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

    }
}
解决方案

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

这篇关于我怎样才能多FlowDocumentReaders添加到StackPanel的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 19:41