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
Related
I have an WPF Textblock to which I bind a view model property, let's say, 'Lines'.
Lines view model property contains three lines separated by the newline character \r\n as below:
IDXXXAZZ000000000099898T<<<<<<\r\n9999999M8888778XXX<<<<<<<<<<<3\r\nXXXX<XXXXX<<XXXXXXX<XXXX<<<<<<
So I am trying to put them in the TextBlock as below:
IDXXXAZZ000000000099898T<<<<<<
9999999M8888778XXX<<<<<<<<<<<3
XXXX<XXXXX<<XXXXXXX<XXXX<<<<<<
As you can see above all three lines always have the same number of characters, 30 characters each one, but for some reason lines does not appear to end at the same point, that is, instead of displaying them like above, they are displayed like below:
I have tried below TextBlock:
<TextBlock Grid.Row="11"
Grid.Column="1" Grid.ColumnSpan="2"
Margin="0 10 0 0"
Text="{Binding Path=Lines}"
FontFamily="Verdana"
FontSize="9" />
So what's the problem? type of font family? font size? I do not understand it.
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"/>
We're developing a Windows Phone 7 app and have a TextBlock defined like so:
<TextBlock Text="{StaticResource MY_TEXT}" TextWrapping="Wrap" Style="{StaticResource SubHeadingStyle}"/>
We have a resource defined like so:
<system:String x:Key="MY_TEXT">This is the first line of text.
This is the second line of text.</system:String>
We've tried all the ways we can think of to represent the line break in the XML, but whatever we try, it's either completely ignored, or appears on screen within the TextBlock.
We've isolated the problem to the loading of the resource dictionary - if we read the string from the resource dictionary programmatically, the line break has been replaced by a space character (0x20). Conversely, if we put a line break character in the Text property of the TextBlock in the XAML, it does appear.
Is there any way we can get the line break to appear correctly in our TextBlock while still using bindings, and without writing code to modify the value?
You have to Preserve WhiteSpace
<system:String xml:space="preserve" x:Key="PURCHASE_SUCCESS">This is the first line of text.
This is the second line of text.</system:String>
Here's my code snippet :
<TextBox Text="{Binding Path=Amount, Mode=TwoWay, StringFormat=\{0:N\}}" />
If the user enters letters or a large number etc, the stringformat dies silently. How can i raise an exception instead ?
Thanks
Bindings swallow exceptions thrown when text input cannot be converted to the data type required by the property on source object. However you can specify ValidatesOnException in the binding. That will cause the standard red border reporting of a conversion problem. BTW this is unrelated to the string format property which is only relevant for displaying the current value, it isn't in play when user is entering data.
<TextBox Text="{Binding Path=Amount, Mode=TwoWay, StringFormat=\{0:N\}, ValidatesOnExceptions=True}" HorizontalAlignment="Left" Width="200"/>
Note I've limited the width and aligned to left. One of the problems with the default validation popup is that is that it is always displayed to the right, which is a bit of a problem when the text box right border is flush with the right edge of the silverlight control's right edge.
Have you thought of writing a filter behavior that allows you to control exactly what goes into the text box?
I'm trying to make a Hyperlink that contains text with super- and/or subscripts. I've found two ways to do this, and both of them suck.
Solution #1: use Typography.Variants. This gives a terrific superscript... for some fonts.
<StackPanel>
<TextBlock FontFamily="Palatino Linotype" FontSize="30">
<Hyperlink>R<Run Typography.Variants="Superscript">2</Run></Hyperlink>
(Palatino Linotype)
</TextBlock>
<TextBlock FontFamily="Segoe UI" FontSize="30">
<Hyperlink>R<Run Typography.Variants="Superscript">2</Run></Hyperlink>
(Segoe UI)
</TextBlock>
</StackPanel>
(source: excastle.com)
Looks beautiful in Palatino Linotype; but for fonts that don't support variants, it's simply ignored, no emulation is done, and the text is full-size, at-baseline, 100% normal. I would prefer to allow my end-users to select the font they want to use, and still have super/subscripts work.
Solution #2: use BaselineAlignment. This raises or lowers the text appropriately, though unlike solution #1, I have to decrease the font size manually. Still, it's effective for all fonts. The problem is the Hyperlink's underline.
<TextBlock FontSize="30" FontFamily="Palatino Linotype">
<Hyperlink>
R<Run BaselineAlignment="Superscript" FontSize="12pt">2</Run>
</Hyperlink>
</TextBlock>
The underline is raised and lowered along with the text, which looks pretty wretched. I'd rather have a continuous, unbroken underline under the whole Hyperlink. (And before anyone suggests a Border, I'd also like the Hyperlink to be able to word-wrap, with all of the words underlined, including the first row.)
Is there any way to make superscript and subscript work in WPF, in any font, without looking laughably bad when underlined?
If the hyperlink isn't going to wrap to more than one line, then embedding another TextBlock can work:
<TextBlock FontSize="30" FontFamily="Palatino Linotype">
<Hyperlink>
<TextBlock>
R<Run BaselineAlignment="Superscript" FontSize="12pt">2</Run>
</TextBlock>
</Hyperlink>
</TextBlock>
This will give a solid hyperlink under the Hyperlink's child, which means an unbroken hyperlink:
However, if the embedded TextBlock needs to wrap to multiple lines, you'll only get one underline under the entire wrapped paragraph, rather than underlining each line of text:
(source: excastle.com)
If you can put a TextBlock only around a short bit of content that needs superscripts -- e.g., around just the R^2 in the above example -- and leave the rest of the text parented to the hyperlink, then you get underlining as normal. But sometimes that's not practical, so it's something to watch out for.
You can use the superscript unicode characters (e.g. http://www.fileformat.info/info/unicode/char/b2/index.htm)
Like this:
<TextBlock FontSize="30" FontFamily="Segoe UI">
<Hyperlink>
Apply R² Calculation
</Hyperlink>
</TextBlock>
Result:
Obviously this will not work unless what you are super scripting actually has a unicode superscript character.