I trying to wrap text at textBox but there are no success..
<Grid Margin="0,0,0,0" BorderThickness="1" BorderBrush="Gray">
<ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Auto" >
<TextBox x:Name="Details" IsReadOnly="True" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Stretch" BorderThickness="0" Height="500" />
</ScrollViewer>
</Grid>
If I have long line without \n I have long string with horizontal scrollbar. But how I understand TextWrapping="Wrap" should cut this string.
I saw this answer but this is not suitable for me because I can have different width of this textBox.
Also I tried to use AcceptsReturn="True" with no success.
I glad to hear any ideas on how to make it works.
I'm not sure if this is what you are looking for, but i'm wondering why you don'disable horizontal Scrolling.
<ScrollViewer Grid.Column="2" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled">
<TextBlock Text="TextusLongusTextusLongusTextusLongusTextusLongusTextusLongusTextusLongusTextusLongus" TextWrapping="Wrap" VerticalAlignment="Stretch"/>
</ScrollViewer>
You don't define any sort of width restriction. Which means that your TextBox can theoratically expand infinitely in width.
Try to either set its width in your XAML code or its maxwidth.
You can also forget about the ScrollViewer. It's already a part of the TextBox and you could just add the elements that you set in your ScrollViewer as an element of the TextBox.
Justt add it like ScrollViewer.VerticalScrollBarVisibility="Visible"
Related
In my program, I have a TextBlock where my result text is shown. In a textbox I can just use TextWrapping="Wrap" and I can scroll down if it's still too much for the textbox. Now in my TextBlock that I talked about I wanted to have the same thing and used TextWrapping="Wrap" again. That works except for the scrolling thing. What can I do now to let it scroll like in the textbox?
Use following structure
<ScrollViewer HorizontalScrollBarVisibility="Auto" x:Name="scrView" VerticalScrollBarVisibility="Auto" Width="100" Height="100">
<TextBlock Width="{Binding Path=ActualWidth, ElementName=scrView}" TextWrapping="Wrap">your text</TextBlock>
</ScrollViewer>
The text in my TextBlock is bound to an element in my code. However, when I first open my window, the Textblock is completely empty. As I add text, I need the ScrollViewer to allow me to scroll down the text, or automatically scroll down as more text is added. Using the MVVM, so no code behind would be ideal.
<StackPanel Grid.Column="0" Grid.Row="1" Margin="0 10">
<Label Style="{StaticResource LabelStyle}">Output</Label>
<ScrollViewer VerticalScrollBarVisibility="Visible" Height="100">
<TextBlock ScrollViewer.CanContentScroll="True" Height="100" VerticalAlignment="Stretch" TextWrapping="Wrap" Text="{Binding Path=ProcessingOutput}"/>
</ScrollViewer>
</StackPanel>
How can I make this happen? Is there a way to update the ScrollViewer so that it sees more text is beyond what I can see in the TextBlock and allows the user to scroll down, or allows me to set an autoscroll feature that scrolls down automatically as text is added via binding?
Thanks in advance!
scrollbar will work if you remove Height="100" from TextBlock
to make it scroll when Text changes other answers suggest to use ScrollViwer.ScrollToBottom() method, e.g. like this:
<ScrollViewer Name="scroll"
VerticalScrollBarVisibility="Visible"
Height="100">
<TextBlock ScrollViewer.CanContentScroll="True"
VerticalAlignment="Stretch"
TextWrapping="Wrap"
Text="{Binding Path=ProcessingOutput, NotifyOnTargetUpdated=True}"
TargetUpdated="Txt_OnTargetUpdated">
</TextBlock>
</ScrollViewer>
private void Txt_OnTargetUpdated(object sender, DataTransferEventArgs e)
{
scroll.ScrollToBottom();
}
As you see, text hidding in textblock, i don't know what is that. Text always are dinamically, so i can't set fixed size.
My XAML code:
<Grid x:Name="Page" Grid.Row="1" Margin="12,0,12,0" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" UseLayoutRounding="True">
<ScrollViewer VerticalContentAlignment="Top" VerticalAlignment="Top" Margin="0,-628,0,0" RenderTransformOrigin="0.5,0.5">
<TextBlock TextWrapping="Wrap" Name="MainContent" UseLayoutRounding="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" Margin="0,42,0,0"/>
</ScrollViewer>
</Grid>
You can try something like this,
Set the ScrollViewer Height to "Auto"
<ScrollViewer Height="Auto" Grid.Row="1">
If it does not work,There is a 2048 pixel limitation for UI controls. Sometimes the text to be displayed is so large that it can’t fit in a TextBlock and some of it overflows. You could create a scrollable textbox as a solution or You need to divide up your text into multiple blocks to display it all.
ScrollableTextBlock
Following XAML code:
<ScrollViewer HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<DockPanel LastChildFill="True">
<TextBox Name="textBox1" DockPanel.Dock="Top" />
<GroupBox Header="All Events" DockPanel.Dock="Top">
<RichTextBox Margin="5" Name="richTextBoxEvents"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto" />
</GroupBox>
</DockPanel>
</ScrollViewer>
causes everything displayed in the RichTextBox vertically!
I want the text to appear horizontally. Why is this appearing vertically and how can I fix that?
I'm using C#, WPF and .NET 4.
EDIT
If the ScrollViewer is taken away, then the text appear horizontally. But I need the scroll viewer. What's the solution then?
A similar scenario can be found here.
Your problem can be solved by setting the width of the RichTextBox.
By setting some arbitrary width.
<RichTextBox Margin="5" Name="richTextBoxEvents"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
Width="100" />
Or you can assign the width of the Parent.
<RichTextBox Margin="5" Name="richTextBoxEvents"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
Width="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType=Window, AncestorLevel=3}, Path=ActualWidth}" />
Here you can change the ancestor level depending on your needs,I bind the RichTextBox width to the Window's width and AncestorLevel is 3.
RichTextBox will always word wrap, so I'm assuming that the ScrollViewer is forcing it to start word wrapping.
If you remove the auto visibility, then it works as expected and you'll still get your scrollbars:
<ScrollViewer>
<DockPanel LastChildFill="True">
<TextBox Name="textBox1" DockPanel.Dock="Top" />
<GroupBox Header="All Events" DockPanel.Dock="Top">
<RichTextBox Margin="5" Name="richTextBoxEvents" />
</GroupBox>
</DockPanel>
</ScrollViewer>
In my ListBox I show different content including text. Text can be long or short. It scrolls by ScrollViewer. Code:
<ScrollViewer MaxHeight="300" VerticalAlignment="Top" Grid.Column="1" Grid.Row="1" >
<TextBlock Style="{StaticResource TextsTextBlock}" Text="{Binding Texts}" Grid.Column="1" Grid.Row="1" />
</ScrollViewer>
and it's also working if text is short, and height of this text do not reach MaxHeight of ScrollViewer. I want to make ScrollViewer works only when text is long and it's height greater than ScrollViewer's MaxHeight, else - it doesn't have to work.
Tried border
<Border BorderBrush="Aqua" BorderThickness="2" MaxHeight="300" VerticalAlignment="Top" Grid.Column="1" Grid.Row="1">
<ScrollViewer VerticalAlignment="Top" VerticalScrollBarVisibility="Auto" >
<TextBlock Style="{StaticResource TextsTextBlock}" Text="{Binding Texts}" Grid.Column="1" Grid.Row="1" />
</ScrollViewer>
</Border>
but it's still scrolls in this border.
Set the VerticalScrollBarVisibility property to Auto.
The default value is Visible which means that the scroll bar is always shown.
By contrast the HorizontalScrollBarVisibility property has a default value of Hidden.
Try removing some of the Grid. properties from the inner controls
<Border BorderBrush="Aqua" BorderThickness="2" MaxHeight="300" VerticalAlignment="Top" Grid.Column="1" Grid.Row="1">
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" >
<TextBlock Text="text" TextWrapping="Wrap"/>
</ScrollViewer>
</Border>
this works fine with one line of text
If I copy paste your code it works fine