Is TextFlow Element Valid in WPF - wpf

For some reason I am not able to use the TextFlow element in WPF. Is this element/control even available?
I am using VS 2008.

If you want to avoid the default chrome of the FlowDocument, you can specify the viewer yourself:
<FlowDocumentScrollViewer>
<FlowDocument>
<Paragraph>
<Run>Hello</Run>
<Run Background="Yellow">World</Run>
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>

There is no TextFlow element.
Were you looking for FlowDocument?
<ContentControl>
<FlowDocument>
<Paragraph>
<Run>Hello</Run>
<Run Background="Yellow"> World</Run>
</Paragraph>
</FlowDocument>
</ContentControl>

You can also use a TextBlock:
<TextBlock><Run>Hello</Run><Run Background="Yellow">World</Run></TextBlock>

Related

Scrolling a FlowDocument inside a TabControl

I have this XAML fragment:
<!-- ... -->
<TabControl>
<TabItem>
<!-- ... -->
</TabItem>
<TabItem Header="Source" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True">
<FlowDocumentScrollViewer>
<FlowDocument>
<Paragraph>
<TextBlock
Text="{Binding Path=CurrentObject.Source}"
FontFamily="Consolas,Courier,Segoe UI"
FontSize="12"
/>
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
</TabItem>
</TabControl>
<!-- ... -->
The problem is that the flow document does not scroll horizontally. I've been unable to enable that.
Any clues?
Thanks in advance.
There are a couple things here. The first being that using a control in the Paragraph functions differently than a Run that would wrap to fit your FlowDocument.
The second is that the FlowDocument will fit your FlowDocumentScrollViewer. If you add a control to it like you did, it will fit the width of the FlowDocument and viewer and the text will go beyond the TextBlock borders. This means that your document doesn't need a scroll bar; your TextBlock would. You can see this by setting the TextWrapping property of the TextBlock to Wrap.
To get around this, set the PageWidth to something beyond the limits of the viewer width like so:
<FlowDocumentScrollViewer>
<FlowDocument PageHeight="1056"
PageWidth="816">
<Paragraph>
<TextBlock
Text="{Binding Path=CurrentObject.Source}"
FontFamily="Consolas,Courier,Segoe UI"
FontSize="12"
/>
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
or bind to your TextBlock:
<FlowDocumentScrollViewer>
<FlowDocument PageHeight="1056"
PageWidth="{Binding ElementName=Part, Path=ActualWidth}">
<Paragraph>
<TextBlock
Text="{Binding Path=CurrentObject.Source}"
FontFamily="Consolas,Courier,Segoe UI"
FontSize="12"
/>
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
The last thing is that the FlowDocumentScrollViewer has it's own HorizontalScrollBarVisibility property that you can use for this (unless some styling issue prevents it).

Add image into a tooltip

I have some TextBlocks with tooltips and I'd like to add an image into the tooltips (that means, I'd like to have tooltips with text and images).
Does anybody knows how could I do that in a simple way?
Thanks a lot!
This is one way to approach it:
<TextBlock>
<TextBlock.ToolTip>
<StackPanel Orientation="Horizontal">
<Image Source="images/Item 2.gif" />
<TextBlock>My tooltip text</TextBlock>
</StackPanel>
</TextBlock.ToolTip>
Here is my text.
</TextBlock>
Instead of
<TextBlock ToolTip="Content"/>
You can do:
<TextBlock>
<TextBlock.ToolTip>
<!--insert everything you want here-->
<TextBlock Text="Content"/>
</TextBlock.ToolTip>
</TextBlock>

Inline object in TextBlock with TextTrimming = CharacterEllipsis or WordEllipsis

If you run this code and minimize/maximize width of the window
<TextBlock TextTrimming="WordEllipsis" >
<Run Text="I want that this rectangle will be placed "/>
<Rectangle Fill="Black" Width="20" Height="10" />
<Run Text=" here when I minimize width of the window"/>
</TextBlock>
you will see what Rectange will shift to the left side.
Is a bug in the WPF?
I guess this is not a bug.
Try to take TextTrimming="WordEllipsis" property off the text block (it affects the whole control)
and you will see that the rectangle wont move with you window size changes beacause you dont have any HorizentalAligment properties.
While it seems to be a bug, this might be a workaround:
<TextBlock TextTrimming="WordEllipsis" >
<Run Text="I want that this rectangle will be placed "/>
<Run Text="■" FontSize="40" BaselineAlignment="Center"/>
<Run Text=" here when I minimize width of the window"/>
</TextBlock>
See Unicode Characters in the Geometric Shapes Block.

WPF. How to align text in InlineUIContainer content with outer text in RichTextBox

The task: Make the text content of the InlineUIContainer to be inline with the outer text
The standard behaviour of the InlineUIContainer content is when the bottom edge is inline with the outer text.
It is possible to shift the content of InlineUIContainer with RenderTransform, but the value of Y has to be chosen for each font type and size - not a perfect way.
<RichTextBox>
<FlowDocument>
<Paragraph>
LLL
<InlineUIContainer>
<Border Background="LightGoldenrodYellow">
<TextBlock Text="LLL"/>
</Border>
</InlineUIContainer>
LLL
</Paragraph>
<Paragraph>
LLL
<InlineUIContainer>
<Border Background="LightGoldenrodYellow">
<Border.RenderTransform>
<TranslateTransform Y="5" />
</Border.RenderTransform>
<TextBlock Text="LLL"/>
</Border>
</InlineUIContainer>
LLL
</Paragraph>
</FlowDocument>
</RichTextBox>
How to align the text in the InlineUIContainer content with the outer text in RichTextBox regardless of font type and size?
have you tried playing around with InlineUIContainer.BaselineAlignment
here are some examples for how to use it

Set superscript and subscript in formatted text in wpf

How can I set some text as subscript/superscript in FormattedText in WPF?
You use Typography.Variants:
<TextBlock>
<Run>Normal Text</Run>
<Run Typography.Variants="Superscript">Superscript Text</Run>
<Run Typography.Variants="Subscript">Subscript Text</Run>
</TextBlock>
You can use something like <TextBlock>5x<Run BaselineAlignment="Superscript">4</Run> + 4</TextBlock>.
However, as far as I know, you will have to reduce the font-size yourself.
It's interesting to note that for some characters (m2, m3, etc) a superscript is not needed, but the unicode character can be used. For example:
<Run Text=" m³" />
This would show m3.
I used a layout transform, because Typography.Variants often doesn't work:
<TextBlock Text="MyAmazingProduct"/>
<TextBlock Text="TM">
<TextBlock.LayoutTransform>
<!-- Typography.Variants="Superscript" didn't work -->
<TransformGroup>
<ScaleTransform ScaleX=".75" ScaleY=".75"/>
<TranslateTransform Y="-5"/>
</TransformGroup>
</TextBlock.LayoutTransform>
</TextBlock>
<TextBlock Text="{Binding Path=Version, StringFormat={} v{0}}"/>
The advantage of using a LayoutTransform is that it is insensitive to the fontsize. If the fontsize is changed afterwards, this superscript works where explicit FontSize setting breaks.
I don't know if you need this to work with FormattedText specifically, or you mean derivations of Inline, but the following will work on Inlines, even if Typography.Variants="Superscript" fails to work.
TextRange selection = new TextRange(document.ContentStart, document.ContentEnd);
selection.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Superscript);
Hope it helps!
Typography.Variants works only for open type fonts. If you dont like your superscripts/subscripts going outside the height of actual text then you can use something like the following:
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="10" Margin="0,5,0,0">1</TextBlock>
<TextBlock FontSize="30">H</TextBlock>
<TextBlock FontSize="10" Margin="0,20,0,0">2</TextBlock>
</StackPanel>
This is the only thing that worked for me. It also gives you more control over the alignment and font size.
<TextBlock Grid.Row="17">
3 x 3<Run FontSize="6pt" BaselineAlignment="TextTop">2</Run>)
</TextBlock>
Setting for superscript works fine with the following code:
<TextBlock Text="(cm" />
<TextBlock ><Span BaselineAlignment="Top" FontSize="8">2</Span></TextBlock>
<TextBlock Text=")" />
Setting the Baseallignment for subscript in the Span tag did not work for me.
I tried the following code and it worked fine.
<TextBlock Text="H" />
<TextBlock Text="2" Margin="-2,0,-2,0" TextBlock.LineHeight="3" >
<TextBlock Text="O" />

Resources