scrollviewer with the textbox - c#

I have a textbox which is contained in a scrollviewer as below:
<ScrollViewer x:Name="myScrollViewer" Height="200" Width="500" HorizontalAlignment="Left">
<TextBox x:Name="myTextBox" Width="500" TextWrapping="Wrap"/>
</ScrollViewer>
When I input a large number of data in the textbox, the scrollviewer will not scroll down automatically, so this lead I couldn't see what I'm inputing now in the textbox, I have to scroll down manully and see the content which I am inputting. I have two questions:
How to let the scrollbar automatically scroll down follow the line which I am writing now.
TextBox has a border, but if I scroll down, the top border will disappear, it looks like the text box is scroll up, how to make the textbox not changes, the 4 borders always appear and only the content wrapped?

Do you need to use a ScrollViewer, or can you use the TextBox's own scrolling behaviour?
This behaves as you would want in normal Silverlight apps (can't test it on a windows 8 app right atm)
E.G.
<TextBox
Height="200"
Width="500"
TextWrapping="Wrap"
AcceptsReturn="True"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto"/>
(Note that you don't seem to be able to set the HorizontalScrollBarVisibility and VerticalScrollBarVisibility properties from a Style)

Related

In a WPF TextBox, why does an inactive selection appear incorrectly when scrolling?

In a WPF TextBox, if I:
set IsInactiveSelectionHighlightEnabled to True
allow horizontal scrolling
type sufficient characters so that I need to scroll the
text box to see them all
The selection highlighting works fine as long as the textbox has focus/is active. However, once it loses focus (but the selection highlight remains due to the IsInactiveSelectionHighlightEnabled being set to true), and if I scroll the text box using the scrollbar, the highlight does not scroll with the text. This essentially means the highlight is now highlighting different characters. If I hit tab to regain focus, the highlight correctly adjusts to highlight the correct characters again.
I can reproduce this very simply, by creating a WPF Desktop App (any .Net framework should be fine), and using this XAML within the Window tag:
<Grid>
<StackPanel>
<TextBox Height="50" Width="100" TextWrapping="NoWrap" IsInactiveSelectionHighlightEnabled="True" HorizontalScrollBarVisibility="Auto" Text="Hello" />
<TextBox Height="50" Width="100" TextWrapping="NoWrap" IsInactiveSelectionHighlightEnabled="True" HorizontalScrollBarVisibility="Auto" Text="World" />
</StackPanel>
</Grid>
With the above layout, just type sufficient characters to make one of the textbox show a scrollbar. Then highlight some characters at the start of that textbox. Then change focus to the other textbox. Then scroll the original textbox and you'll see the selection highlight different/incorrect characters. (You have to look closely since the inactive highlight is a very subtle effect with the default definition).
In other words, the selection highlight does not take the scroll position into account when the textbox is inactive.
I can't imagine this is the intended behavior. Where am I going wrong here?

Windows Phone 8 ScrollViewer.CanContentScroll not recognized?

I am a newbie to Windows Phone 8 app programming in C# and I am trying to create an array of textboxes. I have the array being created and being added as children of a Stack Panel, and I am trying to get it to display more than a few textboxes, and I read that it can be done if the CanContentScroll property is set to 'true' as it is set to 'false' by default. However, when I try to add it, it is not recognised by intellisense. Can you help me?
<ScrollViewer VerticalScrollBarVisibility="Auto" CanContentScroll="True" Margin="10,135,10,7" >
<StackPanel x:Name="TextBoxStack" HorizontalAlignment="Left" Height="419" Margin="0,166,0,0" VerticalAlignment="Top" Width="446"/>
</ScrollViewer>
I am using VS2013, and the System.Phone.Controls and System.Windows.Controls modules are included correctly.
Use a Grid and not a StackPanel. I forget the exact reason, but the StackPanel doesn't correctly report it size to the ScrollViewer container causing the ScrollViewer to not scroll correctly. Using a Grid will resolve this.
You shouldn't need to set "CanContentScroll". The ScrollViewer should display scroll bars if its child extends further than the ScrollViewer's bounds. Try:
Remove the fixed height of the child StackPanel. You don't want to restrict its height -- it should extend as far as its children, so that the ScrollViewer knows the correct scroll extent.
Make sure the ScrollViewer has a fixed or limited height -- ie, put it inside a fixed-size container like Grid, rather than an infinitely-extendable one like StackPanel. If it can extend infinitely, it will always be able to accommodate its child, and won't ever think it has to scroll.
Eg:
<Grid>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel x:Name="TextBoxStack"
HorizontalAlignment="Left" VerticalAlignment="Top" Width="446"/>
</ScrollViewer>
</Grid>

How to change control alignment depending on available space?

Imagine this. I've got a Border which contains some custom wpf control, lets call it MyControl. This Border stretches itself when window is resized (to fill available space). MyControl size is fixed. Now, I want my control to have HorizontalAlignment="Center" when it fits the available space, and HorizontalAlignment="Left" when it doesn't. I'm having trouble figuring out how to implement such behaviour though.
I guess, i can subscribe to Border's SizeChanged event and change alignment in code-behind depending on ActualWidths of Border and MyControl, but isn't there an easier way? Can this be achieved by databinding or by attached behaviour?
It will automatically behave like that if you set the control's Width and Height to fixed values and HorizontalAlignment and VerticalAlignment to Stretch instead of Center:
<Border BorderBrush="Red" BorderThickness="5">
<my:MyControl Width="200" Height="150"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Border>

How to stop ScrollViewer from scrolling down

I need to make the ScrollViewer to only scroll down.
I have created a scrollviewer in Xaml and have populated it with a stackpanel full of rectangles in code. I then start the user at the bottom and want them to use a "walking" motion with their fingers (like a bass player) to scroll to the top but do not want them to be able to scroll back to the bottom.
My Xaml looks like this:
<ScrollViewer Height="730" HorizontalAlignment="Left" Margin="6,6,0,0" Name="scrollViewer1" VerticalAlignment="Bottom" Width="462">
<StackPanel Name="TrackStackPanel">
</StackPanel>
</ScrollViewer>
But since it is filled in code, need to accomplish as much as I can in code.
I would try disabling vertical scrolling via VerticalScrollBarVisibility="disabled" - handle the gestures, then scroll accordingly by setting [ScrollToVerticalOffset].
If this does not work, try placing a layer (a Grid for example) above your ScrollViewer, so that it will receive all the gestures, then do as above, scroll via ScrollToVerticalOffset.

I'm using a WPF TextBlock but then text gets cut off when it's too long. Is there an AutoScroll feature?

My TextBlock has for example 50x50 pixels to display text, however if there is more text, I want a user to be able to scroll. Is there an autoscroll feature for this control?
Should I use a different control better suited for this task?
Here's a couple of pics to illustrate the problem:
This one works fine because the text fits in snugly:
This one doesn't seem correct. Text is cut off.
Just in case someone comes into the same problem. Just wrap the textBlock with a control. Works like a charm!
<ScrollViewer Background="Black">
<TextBlock x:Name="textBlockBackStory"
FontSize="12"
Foreground="Orange"
TextWrapping="Wrap"
Background="Black"
TextDecorations="None">
Backstory here.
</TextBlock>
</ScrollViewer>
You can put your textblock inside a ViewBox, so the font will adjust to display the entire text.

Categories

Resources