简体   繁体   中英

WPF FlowDocument space size is inconsistent

I'm using a FlowDocumentScrollViewer to print out log outputs to my application window, and am getting some rather weird spacing issues. Most of the spaces are correctly sized, but I'll consistently get way too large of spaces in some areas. Here's an example:

Correctly spaced: "d Copying E:\\Projects"
What is displayed: "d Copying E:\\Projects"

I had to use multiple spaces to reflect the spacing issue above, but I guarantee that it is indeed just displaying one space, incorrectly sized. This is easily verifiable by copying the text from my FlowDocumentScrollViewer into a text editor like notepad++. Here's the printing code for my simplest FlowDocumentScrollViewer :

LogBox.Document = new FlowDocument();
LogBox.Document.Background = LogBox.Background;
LogBox.Document.Foreground = LogBox.Foreground;
LogBox.Document.Blocks.Add(logParagraph = new Paragraph());
logParagraph.Margin = new Thickness(0); //Tested making Margin 0, didn't help
logParagraph.FontFamily = font;
logParagraph.FontSize = defaultFontSize;

...

public void PrintLog(String s)
{
    logParagraph.Inlines.Add(s);
}

I've never seen anything like this, and searching for it on google is very difficult since everyone that uses "spacing" as a keyword really means line height... any help is appreciated.

Have you tried...

LogBox.TextAlignment = TextAlignment.Left;

and / or

logParagraph.TextAlignment = TextAlignment.Left;

owo? Because as I can see, in some cases the Block is getting an auto alignments to justify / stretch the text in the line~ Don't know why but seems to happen ;o

You could try adding a Run object in the PrintLog method, and modifying its properties like CharacterSpacing, and maybe try other ones as well. Sorry I don't have access to Visual Studio at the moment so I can't actually try it.

public void PrintLog(String s)
{
     var run = new Run();
     // modify run properties here    
    logParagraph.Inlines.Add(run);
}

You can view the MS documentation here: Run Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM