Silverlight 2.0 - scroll vertically, wrap horizontally - silverlight

In silverlight 2.0. I have some content that i want to scroll vertically and wrap horizontally. In the controls I have a dock panel. The DockPanel's last child, which fills it, is a ScrollViewer
<UserControl x:Class="MyProject.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WinControls="clr-namespace:Microsoft.Windows.Controls;
assembly=Microsoft.Windows.Controls"
Width="400" Height="300">
<WinControls:DockPanel LastChildFill="True">
...
<ScrollViewer x:Name="MessageScroll" HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Auto" BorderThickness="0" >
<Controls:TextDisplay x:Name="TextDisplay"></Controls:TextDisplay>
</ScrollViewer>
The TextDisplay control XAML looks like this:
<UserControl x:Class="MyProject.TextDisplay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextBlock x:Name="TextDisplayText" TextWrapping="Wrap">
</TextBlock>
</UserControl>
What I want to happen: The TextDisplay should occupy the main area of the control,
with a vertical scrollbar if the height doesn't fit. The messages should wrap when they get too long horizontally.
The scrolling works, but now the messages don't wrap at the right-hand edge. they just cut off. It's not constraining the width, just hiding the HorizontalScrollBar. If I set HorizontalScrollBarVisibility="Auto" I can see them scrolling off to the right. How do i force it to wrap?

Try setting the HorizontalScrollBarVisibility of the ScrollViewer to Disabled (or do not specify a value as Disabled is the default) then the TextDisplay will wrap correctly and the horizontal scroll bar will not be displayed.

Related

Xaml - Vertical scrollbar in stackpanel

I have creating a wpf application and in my settings panel I have tons of UI elements. The problem is that when I resize the window some of these elements are not visible anymore. Is there any way to add a simple vertical scrollbar?
I have tried this below and add my content into it :
<ScrollViewer VerticalScrollBarVisibility="Auto">
<Grid>
<StackPanel>
//Content
</StackPanel>
</Grid>
</ScrollViewer>
I'm not sure if I put the ScrollViewer to the right Place but I got this error :
The member resources is not recognized or is not accessible
and for this error I have tried to replace the Page.Resources with Window.Resources but it did not help.
Anyways how could I get my vertical scrollbar working? Any helps?
Problem solved by removing the Width and Height properties from the Page.
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="800" Width="1400"
WindowTitle="ScrollViewer Sample">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<Grid>
<StackPanel>
//Content
</StackPanel>
</Grid>
</ScrollViewer>
</Page>
You should get rid of the StackPanel. A StackPanel measures its children with an infinite space and therefore it doesn't work very well with scroll bars:
Horizontal scroll for stackpanel doesn't work

ScrollViewer inside (Stack-)Panel

I'm fighting with a ScrollViewer inside a StackPanel. The ScrollViewer only shows one scrollbar depending on the StackPanel's orientation, what I kind of understand as the StackPanel thinks to be unlimited in that direction. I therefore tried to limit the size of the ScrollViewer by binding it to the StackPanel's width and height. When the application is brought up it shows both scrollbars but they do not resize properly. What is wrong or how should I do it.
(Remark: I know I can use a Grid instead of the StackPanel and the ScrollViewer behaves as expected. However once I place that Grid into a StackPanel the problem shows up again.)
<Window x:Class="tt_WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="500">
<StackPanel x:Name="sp" Orientation="Horizontal">
<ScrollViewer
HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
Width="{Binding ElementName=sp, Path=ActualWidth}"
Height="{Binding ElementName=sp, Path=ActualHeight}">
<Button Background="LightCoral" Width="500" Height="500">Hey</Button>
</ScrollViewer>
</StackPanel>
A Scroll viewer is useless inside a vertical StackPanel. A vertical StackPanel has its height set to infinity, so the ScrollViewer has all the size it wants and will never show the scrollbar. You should switch to a Grid or DockPanel.
I had the same problem, i solved it by switching stackpanel and scrollviewer.

ScrollViewer with SizeToContent = WidthAndHeight

I have MyWindow where SizeToContent="WidthAndHeight" in WPF. When MyWindow is bigger than screen I want to activate a ScrollViewer. What is the easiest way to do that?
you need to use a... Scrollviewer.
some code for you:
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Name="Window"
SizeToContent="WidthAndHeight">
<ScrollViewer HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="Green">
<Grid Name="MainGrid" Background="red" MinWidth="600" MinHeight="400">
<!--Some Content Here-->
<Label>Foo</Label>
</Grid>
</ScrollViewer>
</Window>
What this code does:
A Window with SizeToContent=WidthAndHeight, as wanted.
A ScrollViewer that stretches with the window
A Grid to put your content (could be a Canvas, or any kind of Panel). This grid as MinWidth and MinHeight set so that what is inside the grid will be able to stretch up but not down. So your content will initially be 600x400 (in this case) but will be able to stretch up. If you try to size it down to 300x200 for instance, you'll get your scrollbars.
this should at least get you started.

Why is this button cut off?

In the following XAML code the button text is half missing. I can change the Margin property and it becomes obvious that after 250px the content is hidden. Why is that, and how can I fix it?
<Window x:Class="InnerInterface.InventoryManagement" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="someWindow" Height="500" Width="500">
<DockPanel HorizontalAlignment="Left" Name="dockPanel1" VerticalAlignment="Top">
<Grid DockPanel.Dock="Top">
<Button Name="buttonReturnToMainMenu" Content="someButton" Margin="200,0" Width="125" />
</Grid>
</DockPanel>
</Window>
You have a horizontal margin of 200, and a button width of 125, which means the total width needed to show the control properly is about 525.
You also have HorizontalAlignment=Left" on your DockPanel, which means it will draw the content at whatever width it needs and align it to the left side of the screen instead of stretching it to fill all available space. This means it is blocking out a space of 200 on either side of the control, and drawing the button in the remaining space. If this remaining space is less than 125, the image will be cropped.
If you switch to HorizontalAlignment="Stretch", then it will draw the control first (with margins), then stretch it's size so it fits all available space, so the entire control gets resized rather than cropped.
You might be interested in reading this MSDN article on Alignment, Margins, and Padding in WPF.
Edit
If you want only the Left margin to be 200, then use Margin="200,0,0,0". Using Margin="200,0" means that both the left and the right Margins will be 200.
Not really sure about your exact problem, but maybe this should help:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="500">
<DockPanel HorizontalAlignment="Stretch" Name="dockPanel1" VerticalAlignment="Top">
<Grid DockPanel.Dock="Top" >
<Button Name="buttonReturnToMainMenu" Content="someButton" Width="125" />
</Grid>
</DockPanel>
</Window>
The problem is that the button Margin is set as:
Margin="200,0"
It should be set as:
Margin="200,0,0,0"
This eliminates the margin on the right side and allows the whole button to show.

How to force a scrollbar to scroll a canvas

I was wondering if there is any way to force a scrollbar to scroll a canvas.
I placed canvas in scrollview and I overrode the measureoverride method. Scrollbars show when I reach the ends of visible parts of the canvas. However I would like the canvas to scroll because now, despite the fact that scrollbars show, the canvas does not follow item. I hope you understand me, sorry for my bad English :)
Let me try,
1) You have a hosted a canvas inside a scroll viewer.
2) When the canvas size increases, the scroll viewer is appearing. [Since, you have set the horizontal or vertical scrollbar visibility to auto]
3) What you want is, When you reach the end of the canvas [Size of canvas gets increases, so scroll bars of scroll viewer will appear.] you want the scroll bar of the scroll viewer to scroll automatically to show the extra space.
If, the above question is right. Here goes, the answer.
You have to do a calculation based on the ActualWidth or ActualHeight of the canvas and set the value to the ScrollToHorizontalOffset or ScrollToVerticalOffset property accordingly.
Below sample will show a red circle in a blue canvas which will have vertical and horizontal scrollbars if it don't fit in the window.
<Window x:Class="WpfApplication.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Canvas Width="500" Height="500" Background="Yellow">
<Ellipse
Stroke="Red" StrokeThickness="10"
Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}"
Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}" />
</Canvas>
</ScrollViewer>
</Window>

Resources