How can I add a newline in the text of a label in WPF such as the following?
<Label>Lorem
ipsum</Label>
<Label><TextBlock>Lorem<LineBreak/>ipsum</TextBlock></Label>
You need to use TextBlock because TextBlock accepts as children a collection of Inline objects. So you are giving the TextBlock element three Inline items: Run Text="Lorem", LineBreak, and Run Text="ipsum".
You can't do the following:
<Label>Lorem<LineBreak/>ipsum</Label>`
because a label accepts one content child element.
Also, not sure exactly what your use case is but notice I placed a TextBlock inside your Label element. Is it repetitive? Not really, depending on your need. Here's a good article on the differences between the two elements: Difference between Label and TextBlock
in WPF you can use the value "
" or "
"
For example:
<Label Content="Lorem
ipsum" />
("10" is the ASCII number for newline)
or
<Label Content="Lorem
ipsum" />
("A" is the ASCII number for newline in hex)
When doing this in the ViewModel or Model, I have found that using Environment.NewLine has the most consistent outcome, including localization. It should work directly in the View as well, but I haven't tested that.
Example:
In the View
<Label Content="{Binding SomeStringObject.ParameterName}" />
In the ViewModel:
SomeStringObject.ParameterName = "First line" + Environment.NewLine + "Second line";
An example of how to add a ToolTip with multiple lines to a control, such as a button. The tooltip is width limited so it will wrap if a sentence is too wide.
<!-- Button would need some properties to make it clickable.-->
<Button>
<Button.ToolTip>
<TextBlock Text="Line 1
Line 2" MaxWidth="300" TextWrapping="Wrap"/>
</Button.ToolTip>
</Button>
Tested on VS2019 + .NET 4.6.1 + WPF.
<Label xml:space="preserve">text content
another line</Label>
seems to work too
Related
In my wpf application, I have added a text block control and set its value with Chinese and English text. But it wraps and I don't want it to wrap.
How can I resolve this problem?
Add "TextBlock.TextAlignment Property" Like
<TextBlock Name="tbllist" Text="Your text" TextWrapping="Wrap" TextAlignment="Justify"/>
I have multiple TextBlocks in a control. The blocks have a fixed width and the TextWrap property is set to Wrap. The text is provided via binding.
Right now the wrapping occurs when SL detects that it can't fit another character in the line. Which results in something like "The quick bro" \r\n "wn fox jumps".
But I want those blocks to wrap their text only at word boundaries and not at some random position in the middle of a word. The expected outcome should look something like "The quick brown" \r\n "fox jumps".
This is the XAML for one of the TextBlocks:
<TextBlock
x:Name="Foo"
Foreground="#FFD4E4FF"
FontSize="14.667"
FontFamily="Arial"
Canvas.Left="586.671"
LineHeight="23.707"
TextWrapping="Wrap"
Text="{Binding Bar}"
Canvas.Top="170"
Width="120" />
Any ideas?
We finally found the problem. For some reason the strings we loaded from the database contained characters that looked like regular blanks in the debugger and in text editors but where not treated as such by Silverlight. The character in question was a non-breaking space
I would like to be able to change the weight of text (e.g. change from Normal to Bold and back again) within the Text property string of a TextBlock (presumably using some control character set). Is this even possible?
TextBLock.Text creates a single Run, you set custom Inlines instead:
<TextBlock>
Text with <Bold>bold</Bold> within.
<TextBlock>
Obviously it no longer uses the Text property.
Are you talking about something like this?
<TextBlock>
<Run Text="Hey it's Normal Text"/>
<Run Text="Hey it's Bold Text" FontWeight="Bold"/>
<Run Text="Hey it's Colored Text" Foreground="Green"/>
</TextBlock>
I have many FrameworkElements (TextBlock, CheckBox, ListBox..) and I would like to make something allowing me to show a small number besides every one control.
Some text ³
I came with the idea to write a MarkupExtension, where I could write that number like this:
..
<TextBlock Text="Some Text" SomeExtension="3" />
..
and then to add it somehow to the template of the Control.
But I'm sure, you guys have better solution for this problem ;)
One way to go with it would be create a Attached Property. Upon setting it on a control, a custom Adorner would be added for that control showing specified number.
Use the tag property to provide the number you want and inside the custom template databind to the property
<TextBlock Text="Some Text" Tag="3" />
and inside the controltemplate
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}"/>
When would I use the Text attribute of the <TextBlock> and when should I put my text in the content of the <TextBlock>?
<TextBlock Text="Example Text" />
vs.
<TextBlock>Example Text</TextBlock>
The former can be bound, whilst the latter is particularly useful when combining Runs:
<TextBlock Text="{Binding SomeProperty}"/>
<TextBlock>
<Run>You have </Run>
<Run Text="{Binding Count}"/>
<Run>items.</Run>
</TextBlock>
The use of the Text property has become common as a result of previous versions of the Xaml parser but the placing the text as content is more natural especially if you have a background in HTML.
The fact the many TextBlocks either have simple short chunks of literal text in or are bound. Would tip the balance IMO to using the Text property. In addition any globalisation that may come along latter may end with those literals being replaced by bindings as well.