Reputation: 563
I would like to be able to remove the white spaces between various inline elements of a FlowDocument. Below is a very specific example just to make the problem clear. The desired output is "Hello World?" but what happens is "Hello World ?". In this case "?" is a clickable button.
I have searched for a while without success. I tried all forms of pad/margin adjustment, but they can only increase the spacing. I am beginning to believe the space is inherent to FlowDocument element boundaries. This seems like a series limitation.
<RichTextBox>
<FlowDocument>
<Paragraph Margin="0">
<Run>
Hello World
</Run>
<InlineUIContainer>
<Button Click="ButtonClick">?</Button>
</InlineUIContainer>
</Paragraph>
</FlowDocument>
</RichTextBox>
Upvotes: 2
Views: 3997
Reputation: 345
Ok, this problem is beacuse of special whitespace handling rules.
Idea of the solution: Preprocess xaml removing new line/whitespace characters between the tags.
Implementation:
Load it, remove whitespaces using the following code:
var uri = @"pack://application:,,,/YourProjectName;component/PathToDictionary/Strings.xaml";
var resourceInfo = Application.GetResourceStream(uri);
using (var xmlReader = new XmlTextReader(resourceInfo.Stream) { WhitespaceHandling = WhitespaceHandling.None})
{
var xamlReader = new System.Windows.Markup.XamlReader();
dictionary = (ResourceDictionary)xamlReader.LoadAsync(xmlReader);
}
Add the resource dictionary to the resources of the entity, wich should use the Paragraph:
MyControl.Resources.MergedDictionaries.Add(dictionary);
That`s all, no additional Runs with whitespaces inside will be created.
I`m using our own custom localizable ResorceDictionary descendant, wich has an option to remove whitespaces and is processing all this stuff automatically.
Upvotes: 2
Reputation: 1730
As ugly as the formatting might seem, in XML based XAML, if you want to avoid whitespace between runs you cannot allow any space between the close and open tags of the elements. Try this:
<RichTextBox>
<FlowDocument>
<Paragraph Margin="0">
<Run>
Hello World
</Run><InlineUIContainer>
<Button Margin="0" >?</Button>
</InlineUIContainer>
</Paragraph>
</FlowDocument>
</RichTextBox>
Upvotes: 5
Reputation: 401
You could add negative margins to the button to make it fit closer:
<InlineUIContainer><Button Margin="-3,0">?</Button></InlineUIContainer>
Upvotes: 0