I am having a few problems with the TextBlock control.
Problem 1:
I am trying to get the scrollbar to work. I could not find an option to turn on or off scrollbars on the TextBlock control so I grudgingly embedded my TextBlock in a ScrollViewer control, but even that does not seem work. Does anyone know how to do this?
The text just stops when I run out of room.
Unfortunately TextBlock itself doesn't support Scrolling. In WPF we have FlowDocumentScrollViewer, but that's not the case with Silverlight... You can wrap the TextBlock inside a ScrollViewer. Then you need to set both the Horizontal/VerticalScrollBarVisibility
to Auto, so the ScrollBar will appear when appropriate. Also generally speaking, you'll want to set a specific Width to the TextBlock as well as set TextBlock.TextWrapping to true. By doing so, you won't need to scroll horizontally, but if the text is too
long, the vertical ScrollBar will still appear. Most users like this behavior.
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<TextBlock Text="Write a very long text here..." TextWrapping="Wrap" Width="300"/>
</ScrollViewer>
For text formatting, if you're using Expression Blend, just right click the TextBlock and choose Edit Text. Then you can select some text and do the formatting. If you want to manually write XAML, you should write a series of Run tags instead of Text.
<TextBlock>
<Run Text="Make only the word"/>
<Run FontWeight="Bold" Text=" bold "/>
<Run Text="appear in"/>
<Run FontWeight="Bold" Text="bold"/>
</TextBlock>
If you want to do that in code, that's quite a lot of work...
TextBlock tb = new TextBlock();
Run run1 = new Run() { Text = "Only make the word " };
Run run2 = new Run() { Text = "bold", FontWeight = FontWeights.Bold };
Run run3 = new Run() { Text = " appear in " };
Run run4 = new Run() { Text = "bold", FontWeight = FontWeights.Bold };
tb.Inlines.Add(run1);
tb.Inlines.Add(run2);
tb.Inlines.Add(run3);
tb.Inlines.Add(run4);
shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
The ScrollViewer will only scroll if the TextBlock inside it has a height larger than the ScrollViewer's. The problem is, you have to explicitly set the height of the TextBlock. It will not resize itself based on its content at runtime. When the TextBlock
fills up with text it will cut the text off instead of expanding in height. That is the root cause of my troubles.
To get around this, I wonder if there is a control I could put in the ScrollViewer that does increate in size dynamically at runtime based on its contents. Perhaps useing a Stackpanel inside a ScrollViewer. Inside the Stackpanel dynmaically add new TextBlock
objects in code when new text needs to be added. I have no idea if this makes since or how I would go about doing it.
Have you tried checking the ActualHeight property instead of just the Height property? I have had some recent issues with ScrollViewer as well. One way that I solved the problem was to make sure that the Height and Width properties of my object which
was contained inside of the scrollviewer were set to the same as ActualHeight and ActualWidth.
Thanks davidpni! Now scrolling is working, however when text is inserted the ScollViewer does not automatically go to the bottom of the TextPanel. The new text added is hidden at the bottom while the top of the TextBlock stays in view at the top. Do you
happen to know how to make the ScrollViewer deualt to the bottom instead of the top so you can see the next text as it is added?
I have the same problem width scrolling textblock inside a scrollviewer. Here is my code, and I cannot understand whats wrong! I have replaced real text with Mytext.
StargateFan
Member
17 Points
43 Posts
Few problems using TextBlock (Scrolling and Formatting)
Jun 12, 2008 03:50 AM | LINK
I am having a few problems with the TextBlock control.
Problem 1:
I am trying to get the scrollbar to work. I could not find an option to turn on or off scrollbars on the TextBlock control so I grudgingly embedded my TextBlock in a ScrollViewer control, but even that does not seem work. Does anyone know how to do this? The text just stops when I run out of room.
<ScrollViewer HorizontalAlignment="Stretch" Margin="125,48,16,72" VerticalAlignment="Stretch">
<TextBlock Text="Simon: Hello!" TextWrapping="Wrap" FontSize="12" x:Name="TextblockChat" Height="104" Width="232"/>
</ScrollViewer>
Problem 2:
I can not figure out to how to format a string in C# when setting the Text property of the TextBlock
TextBlock.Text = "Make only the word <b>bold</b> appear in <b>bold</b>";
I can really use the help if you anyone knows. Thanks!
StargateFan
Member
17 Points
43 Posts
Re: Few problems using TextBlock (Scrolling and Formatting)
Jun 12, 2008 02:58 PM | LINK
Anyone have any ideas? I will mark the answer. :(
This is just displaying text. Is Silverlight version 2 this incomplete?
Yi-Lun Luo -...
All-Star
25149 Points
2759 Posts
Microsoft
Re: Few problems using TextBlock (Scrolling and Formatting)
Jun 13, 2008 05:44 AM | LINK
Unfortunately TextBlock itself doesn't support Scrolling. In WPF we have FlowDocumentScrollViewer, but that's not the case with Silverlight... You can wrap the TextBlock inside a ScrollViewer. Then you need to set both the Horizontal/VerticalScrollBarVisibility to Auto, so the ScrollBar will appear when appropriate. Also generally speaking, you'll want to set a specific Width to the TextBlock as well as set TextBlock.TextWrapping to true. By doing so, you won't need to scroll horizontally, but if the text is too long, the vertical ScrollBar will still appear. Most users like this behavior.
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<TextBlock Text="Write a very long text here..." TextWrapping="Wrap" Width="300"/>
</ScrollViewer>
For text formatting, if you're using Expression Blend, just right click the TextBlock and choose Edit Text. Then you can select some text and do the formatting. If you want to manually write XAML, you should write a series of Run tags instead of Text.
<TextBlock>
<Run Text="Make only the word"/>
<Run FontWeight="Bold" Text=" bold "/>
<Run Text="appear in"/>
<Run FontWeight="Bold" Text="bold"/>
</TextBlock>
If you want to do that in code, that's quite a lot of work...
TextBlock tb = new TextBlock();
Run run1 = new Run() { Text = "Only make the word " };
Run run2 = new Run() { Text = "bold", FontWeight = FontWeights.Bold };
Run run3 = new Run() { Text = " appear in " };
Run run4 = new Run() { Text = "bold", FontWeight = FontWeights.Bold };
tb.Inlines.Add(run1);
tb.Inlines.Add(run2);
tb.Inlines.Add(run3);
tb.Inlines.Add(run4);
StargateFan
Member
17 Points
43 Posts
Re: Few problems using TextBlock (Scrolling and Formatting)
Jun 13, 2008 06:44 PM | LINK
The ScrollViewer will only scroll if the TextBlock inside it has a height larger than the ScrollViewer's. The problem is, you have to explicitly set the height of the TextBlock. It will not resize itself based on its content at runtime. When the TextBlock fills up with text it will cut the text off instead of expanding in height. That is the root cause of my troubles.
To get around this, I wonder if there is a control I could put in the ScrollViewer that does increate in size dynamically at runtime based on its contents. Perhaps useing a Stackpanel inside a ScrollViewer. Inside the Stackpanel dynmaically add new TextBlock objects in code when new text needs to be added. I have no idea if this makes since or how I would go about doing it.
davidpni
Member
66 Points
82 Posts
Re: Few problems using TextBlock (Scrolling and Formatting)
Jun 13, 2008 07:04 PM | LINK
Have you tried checking the ActualHeight property instead of just the Height property? I have had some recent issues with ScrollViewer as well. One way that I solved the problem was to make sure that the Height and Width properties of my object which was contained inside of the scrollviewer were set to the same as ActualHeight and ActualWidth.
StargateFan
Member
17 Points
43 Posts
Re: Few problems using TextBlock (Scrolling and Formatting)
Jun 14, 2008 02:18 AM | LINK
Mark Rideout
Contributor
2736 Points
307 Posts
Microsoft
Moderator
Re: Few problems using TextBlock (Scrolling and Formatting)
Jun 14, 2008 03:09 AM | LINK
Can you use a TextBox? This comes with scroll bars, text wrapping. You can make the TextBox ReadOnly also.
-mark
Silverlight Program Manager
Microsoft
This post is provided "as-is"
StargateFan
Member
17 Points
43 Posts
Re: Few problems using TextBlock (Scrolling and Formatting)
Jun 14, 2008 03:18 AM | LINK
Hi Mark, not in this case. I need to be able to format the text.
leakybagel
Member
20 Points
10 Posts
Re: Re: Few problems using TextBlock (Scrolling and Formatting)
May 07, 2009 05:50 PM | LINK
To get the scroller to view the bottom line of text in a textblock, whenever new text is added:
myScroller.ScrollToVerticalOffset(myScroller.ScrollableHeight);
vind-surfer
Member
3 Points
12 Posts
Re: Few problems using TextBlock (Scrolling and Formatting)
Mar 28, 2010 04:57 PM | LINK
Hi!
I have the same problem width scrolling textblock inside a scrollviewer. Here is my code, and I cannot understand whats wrong! I have replaced real text with Mytext.
<ScrollViewer Margin="203,8,8,8" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<ScrollViewer.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="Black" Offset="1"/>
<GradientStop Color="#FF590F0F" Offset="0.003"/>
<GradientStop Color="#FF591111" Offset="1"/>
</LinearGradientBrush>
</ScrollViewer.BorderBrush>
<TextBlock TextWrapping="Wrap" Width="560"><Run FontSize="21.333" Foreground="#FFAD2323" Text="Mytext"/><LineBreak/><Run FontSize="13.333" Text="Mytext."/><LineBreak/><Run FontSize="13.333" Text=""/><Run FontSize="21.333" Foreground="#FFAD2323" Text="Mytext"/><LineBreak/><Run FontFamily="Verdana" FontSize="21.333" Foreground="#FFAD2323" Text="Mytext."/><LineBreak/><Run FontFamily="Verdana" FontSize="13.333" Text="Mytext."/><LineBreak/><LineBreak/><Run FontSize="21.333" Foreground="#FFAD2323" Text="HP "/><LineBreak/><Run FontSize="13.333" Text="Mytext."/><LineBreak/><Run FontSize="13.333" Text="Mytext"/><LineBreak/><Run FontSize="13.333" Text="Mytext."/><LineBreak/><LineBreak/><Run FontSize="21.333" Foreground="#FFAD2323" Text="Mytext"/><LineBreak/><Run FontFamily="Verdana" FontSize="13.333" Text="MyText."/><LineBreak/><Run FontSize="21.333" Foreground="#FFAD2323" Text=""/></TextBlock>
</ScrollViewer>
Best regards
Jimmy