Can I limit WPF TextBlock height to two lines? - wpf

I just received a requirement to display a length of text in a control. The control is of a particular width and will be up to 2 lines in height. If it renders longer than two lines it will just display "..." at the end of the string.
Is this possible with any of the stock standard WPF controls?
Thanks,
D.

Set the Height of the TextBlock to be high enough to fit two lines. Set the TextWrapping to Wrap and the TextTrimming to CharacterEllipsis or WordEllipsis.
For the default Segoe UI 12Pt font, I find this does it
<TextBlock TextWrapping="Wrap" Height="40" TextTrimming="CharacterEllipsis" />
You could probably do some code behind to work out the height it should be be for a particular font if you want.

That's not standard behavior that I've ever found, but again I've not looked for it.
One possibility is to use a monospace font in a TextArea control, and then if the string is greater than however many characters fit in the area, only display the right N characters with the ellipses

Related

Trying to wrap and scale text in a textblock WPF

I'm trying to auto scale the font and wrap text in a TextBlock in WPF and I can't figure it out.
I've Googled it and looked at stackoverflow loads of times and the main suggestion is to place a TextBlock inside a ViewBox. I've tried that, and all it does is scale the whole text down to one line instead of wrapping it.
If I just use a TextBlock without a ViewBox it wraps, but doesn't scale to fit. It's driving me mad, as I am literally trying to move from WinForms to WPF to make better looking UIs.
I've tried StackPanel and DockPanel and they still don't have the desired effect.
All I want is a TextBlock to take a string of text of unknown size and display it scaled and wrapped. I don't understand why it's so difficult
It is helpful to include code of what you have tried.
When I do this:
<Grid>
<TextBox VerticalAlignment="Center" TextWrapping="Wrap" Width="100"/>
</Grid>
I get this:
Is that what you are looking for?
You could also check out the RichTextBox if you need more features.

Is there any difference between WPF TextBlock and TextBox?

What criteria must I consider when selecting one of these two controls?
Common to both TextBlocks and TextBoxes:
Can be used to display text
Can be set to specific Height and Width or be set to Auto so that they grow in size with the text.
Can set font size, font type, font styling, to wrap and to range left, right or centred.
Can have opacity set and have Pixel Shaders applied.
TextBlock:
Used for displaying text more focused typographically.
Can contain text set to different colors, fonts and sizes.
The line height can also be increased from the default setting to give more space between each line of text.
Text inside a TextBlock cannot be made selectable by the user.
TextBox:
Used for displaying text more focused for content input or when content is needed to be made selectable by the user.
Can only be set to one colour, one font size, one font type etc.
Have fixed Line Spacing.
Can also be set to a fixed height and width but also have scrollbars switched on to allow content to expand.
TextBlock is more lightweight control for displaying text and TextBox is used when you require user input or edit existing text. Proof for mem usage.

How do I make a TextBox that can grow wider than its parent?

Is there any way I can make a WPF TextBox dynamically grow beyond the bounds of its parent?
For example, I am allowing a user to type in an XPath string that could be very long (wide), I would like one of two possible things to happen:
The textbox could grow wider when the user types a certain amount of characters.
or
When the user initially clicks on the textbox, it kind of 'pops-out' and is very wide, wider than its container.
Is this possible?
You can set a negative Margin to allow it to grow wider than its parent.
<Grid>
<Grid Margin="50">
<TextBlock Margin="0,0,-50,0" Text="This is a very long text." />
</Grid>
</Grid>
You can try the following:
Put both the ParentControl and the TextBox inside a Canvas.
Locate the TextBox using fixed or dynamic coordinates relative to the ParentControl.
Set the TextBox Z-Index to higher value of the ParentControl.
Relocate and/or expend the TextBox based on TextBox events.

How do I move the text in a silverlight textbox

I make a silverlight TextBox. The Text is vertically centered in the box so that I am required to make the textbox a certain size in order to see the text.
Can I make the text appear at the top of the box so that I am not required to have such large textboxes?
If you want to put the text at the top of the textbox then instead of VerticalAlignment="Center" use VerticalAlignment="Top"
If you feel that the text is not filling the textbox then try using a negative number for the padding property in your xaml e.g. Padding="-3"

Silverlight scrolling text & max width issue

I am trying to scroll text across the screen which is working well.
Update: I'm still stuck with problem and can now demonstrate it on my live app:
Go to
http://www.pokerdiy.com/poker-blinds-timer.aspx
and leave it non-fullscreen.
Click on the "Timer" tab at the top.
Then click on "Start Tourney". At
the top, a scrolling message will
appear from the right. On my monitor
(22 inch) - this cuts off before it
has finished the sentence.
Then right-click to resize to full
screen, and restart the tourney and
then start it again to show the same
message. You will see that it now
works!
This is a big problem as I want to scroll LONG messages across the top... any ideas please?
Problem Summary:
I have a Textblock off the screen to the right which has no width set and is bound to a string value. I programatically start an animation which changes the From and To value on CompositeTransform.TranslateX to move the whole textblock across the screen to give the appearance of scrolling. The width of this textblock autoadjusts to the bound string value and I set this to be the Animation To value (negative) so it takes it off the screen to the left (see code below).
So this all works fantastically... BUT not with long messages. There seems to be a width limit on something that truncates my text. At some point it cuts it off with a width limit (the last character is rendered in half, so it is not a character limit). I set up a loop to create a large string and output the ActualWidth of the Textblock and it shows as 17000 (which is correct). The Width is fine too, and the actual Text property shows the complete string... but the UI truncates it at a certain point, which I can't figure out.
So - 1) Is this approach ok (is there an easier way?) and 2) What is causing the truncation?
Thanks!
Xaml:-
<Storyboard x:Name="StoryboardScrollText" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)">
<DoubleAnimation x:Name="StoryboardScrollTextAnimation" Storyboard.TargetName="txtSystemMessage" From="500" To="-740" Duration="0:0:30" />
</Storyboard>
<TextBlock x:Name="txtSystemMessage" TextWrapping="NoWrap" Text="{Binding TourneyMessage}" Foreground="White" FontSize="20" VerticalAlignment="Center" >
<TextBlock.RenderTransform>
<CompositeTransform TranslateX="0"/>
</TextBlock.RenderTransform>
</TextBlock>
Code:-
mainPage.StoryboardShowTourneyMessage.Begin();
//has to scroll the whole message off the screen (plus a bit extra as it starts off the screen)
//get the leftmost position of the logo so it starts just behind it
mainPage.StoryboardScrollTextAnimation.From = GetPositionX(mainPage.NavigationGridLogo);
mainPage.StoryboardScrollTextAnimation.To = mainPage.txtSystemMessage.ActualWidth * -1 - 50;
mainPage.StoryboardScrollText.Begin();
MessageBox.Show(mainPage.txtSystemMessage.ActualWidth.ToString() + " " + mainPage.txtSystemMessage.Width.ToString());
Part 1: If you intend to do pixel based
animation, use a canvas as the
parent object. Grid will work, but see the cause of your problem in part 2 (below)
Part 2: Basically the truncation of your
text is caused by the parent
container cropping the child. This
is happening based on width of the
parent and it ignores the offset of
your text.
Solution:
If you place the TextBlock as a child
of a canvas, instead of a grid (or even just the TextBlock within a canvas within your grid), it will render
full length as a canvas will never clip its children by default.
You may need to add a clip rectangle
to the canvas to hide any parts of
the text that extend where you do
not want to see it, but this will depend on what other components are onscreen to hide the overlap. If you do add a clip rectangle, make sure you animate the text position using Canvas.Left and not the TranslateX or you may get the original problem back again!

Resources