ScrollViewer does not display "bar" - wpf

I have an ItemsControl wrapped in a ScrollViewer. The ScrollViewer behaves correctly but does not display correctly:
I have no Styling for ScrollViewer anywhere in my app.
The bar size looks like 0px, so the arrows end up touching.
This happens with every ScrollViewer enclosing an ItemsControl in my app.
I tested a simple ItemsControl to remove possibility of other style information effecting it:
<ScrollViewer VerticalScrollBarVisibility="Visible">
<ItemsControl>
<TextBlock Text="test"/>
<TextBlock Text="test"/>
<TextBlock Text="test"/>
</ItemsControl>
</ScrollViewer>
and the result was the same. I also wrapped ItemsControl in a WrapPanel and set Min/Max Heights on the ScrollViewer and WrapPanel and there was no change.
Does anyone know why this is happening and how I can fix it?
Edit:
I looked at the ScrollViewer with snoop and the bizarrely Snoop's ScrollViewers also displayed incorrectly (no track bar):
Thanks

Related

Why is a Border needed around a TextBlock in an ItemsControl template to make the TextBlock wrap?

I found this question while trying to figure out how to make a TextBlock wrap, when that TextBlock is the template for each item in an ItemsControl.
My original template:
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Padding="2" x:Name="SummaryRow" Text="{Binding}" TextWrapping="Wrap" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
In order to make the text actually wrap, I had to surround the TextBlock with a Border. I'm sure other containers would have worked as well.
Why is this? (btw I should mention that the ItemsControl is in a ScrollViewer)
For the text to be wrapped you need to restrict the size of the textBlock so as to wrap the text once it exceeds that restricted limit. But since your textBlock have scrollViewer outside, it has no restiction on its size and hence no wrap. You need to set the HorizontalScrollBarVisbility to Collapsed or Hidden to restrict the size and hence wrapping of text.

WPF DataGrid MaxWidth set to 100% availiable space

I've got unusual problem that I can't solve. I want to use DataGrid with fixed column sizes that will size up to parent. Structure looks more less like this:
UserControl put on ScrollViewer sizes correctly to parent unless it reaches MinWidth then scrollbar appears. All controls on UserControl are stretched when someone resize window. Only individual is DataGrid. It doesn't want to enable horizontal scroll no matter how much I try it always makes UserControl scale to it's width. This of course enables scroll under UserControl which I want to prevent.
in short I want DataGrid to take up 100% of available width but not more.
<Window>
<ScrollViewer>
<UserControl>
<Grid>
<DataGrid>
</DataGrid>
</Grid>
</UserControl>
</ScrollViewer>
</Window>
I also tried to do something like that, but it failed :(
<Window>
<ScrollViewer>
<UserControl>
<Grid>
<ScrollViewer>
<DockPanel>
<DataGrid>
</DataGrid>
</DockPanel>
</ScrollViewer>
</Grid>
</UserControl>
</ScrollViewer>
</Window>
Also hooking up to SizeChanged and initially setting Width of DataGrid to some small fixed value event did not make the test. Probably because I'm operating on UserControl derived code.
I need suggestions.
Get rid of the ScrollViewer around the DataGrid entirely, that will cause the DataGrid to not know the width of its parent control - it tells the DataGrid that it has infinite space.
Just try placing the DataGrid as in your first example, and setting HorizontalAlignment=Stretch on the DataGrid. That should be the default on the Grid containing the DataGrid, but set it there too just to be sure. Also make sure you don't have the width property set (MinWidth should be fine).
Edit your code into your question once you've given it a shot.

Unable to get vertical scroll bars in an WPF TextBlock

I'm presenting text in a wpf TextBlock control (.Net 3.5). The content of the textblock varies depending on what the user selects in a list box. The text wraps, so I don't need an horizontal scroll bar. However, there is often more text than the amount the window can display, so I need a vertical scroll bar.
As I started searching I quickly found that the answer is to wrap the TextBlock in a ScrollViewer. However, It Does Not Work (TM) and I'm hoping someone can help me work out why.
This is the structure of the UI code:
<Window x:Class=..>
<StackPanel>
<ListBox HorizontalAlignment="Stretch"
VerticalAlignment="Top" Height="200"
SelectionChanged="listbox_changed" SelectionMode="Single">
</ListBox>
<Button Click="Select_clicked">Select</Button>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBlock Name="textblock" TextWrapping="Wrap"/>
</ScrollViewer>
</StackPanel>
</Window>
When the user selects an item in the list box, some text associated with this item is presented in the TextBlock. I would have thought that the code as it stands should have been all that's required, but it never provides me with a scroll bar.
Searching and experimenting have given me two clues: the root of the problem might be related to me updating the content of the TextBlock dynamically, and that the TextBlock does not resize itself based on the new content. I found a posting that seemed relevant that said that by setting the Height of the TextBlock to its ActualHeight (after having changed its content), it would work. But it didn't (I can see no effect of this).
Second, if I set the height (during design time) of the ScrollViewer, then I do get a vertical scroll bar. For instance, if I set it to 300 in the xaml above, the result is almost good in that the window as first opened contains a TextBlock with a vertical scroll bar when (and only when) I need it. But if I make the window larger (resizing it with the mouse during runtime), the ScrollViewer does not exploit the new window size and instead keeps its height as per the xaml which of course won't do.
Hopefully, I've just overlooked something obvious..
Thanks!
Because your ScrollViewer is in a StackPanel it will be given as much vertical space as it needs to display it's content.
You would need to use a parent panel that restricts the vertical space, like DockPanel or Grid.
<DockPanel>
<ListBox DockPanel.Dock="Top" HorizontalAlignment="Stretch"
VerticalAlignment="Top" Height="200"
SelectionChanged="listbox_changed" SelectionMode="Single">
</ListBox>
<Button DockPanel.Dock="Top" Click="Select_clicked">Select</Button>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBlock Name="textblock" TextWrapping="Wrap"/>
</ScrollViewer>
</DockPanel>

how do I stick the scrollbars of ScrollViewer to top in silverlight?

I have an Itemscontrol in my xaml inside a ScrollViewer.
<ScrollViewer Margin="0,0,0,0" BorderThickness="0">
<ItemsControl x:Name="itemsStackPanel">
<ItemsControl.ItemTemplate>
<DataTemplate>
<controls:UserItem Margin="0, 5, 0, 3"></controls:UserItem>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
and here is the list I am assigning to ItemsControl,
this.itemsStackPanel.ItemsSource = usersList;
now whenever new items are added in usersList, the UI updates and the scrollbar of ScrollViewer reaches at bottom. how do I stick it to top??
--EDIT--
one more issue I found, is that when ever the scrollviewer is resized horizontally, the scrollbars reach at bottom. how to keep the scrollbars on top while resizing?
One way would be to call
scrollView.ScrollToVerticalOffset(0);
MSDN page
However, this might just cause the list to scroll to the bottom and then the top again - not what you want.

Scrollbar in Listbox not working

I have a ListBox that displays a list of WPF controls.
My problem is that the vertical scrollbar is show but is disabled even when there are enough items that the ListBox should be scrollable.
One other possibly relevant fact is that this is contained in an Integration.ElementHost.
WPF noobie, Jim
Here is the XAML for the ListBox:
// for brevity I removed the Margin and Tooltip attributes
<Grid x:Class="Xyzzy.NoteListDisplay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Name="stackPanel" Orientation="Vertical"
ScrollViewer.VerticalScrollBarVisibility="Visible">
<StackPanel Orientation="Horizontal">
<CheckBox Name="AllRecent" IsChecked="False" >View All Recent</CheckBox>
<CheckBox Name="AscendingOrder" IsChecked="False">Descending Order</CheckBox>
<Button Name="btnTextCopy" Click="btnCopyText_Click">Copy All</Button>
</StackPanel>
<ListBox Name="NoteList"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Visible">
</ListBox>
</StackPanel>
</Grid>
And the XAML for the control displayed in each ListBox item:
<UserControl x:Class="Xyzzy.NoteDisplay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Name="Heading" FontSize="10">Note Heading</TextBlock>
<Button Name="btnCopyText" Height="20" FontSize="12"
Click="btnCopyText_Click">Copy
</Button>
</StackPanel>
<TextBlock Name="Body" FontSize="14">Note Body</TextBlock>
</StackPanel>
</Grid>
</UserControl>
I have had problems with scroll bar visibility when using a StackPanel. I think it is because the StackPanel is always as big as it needs to be to contain all of its children. Try reorganizing the layout to remove the StackPanel (use a Grid instead) and see if that helps.
You just need to introduce Height property, like this:
<ListBox Height="200"
Name="NoteList"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Visible">
</ListBox>
Heya, I suspect what might be happening is that your ListBox is expanding enough for every item however the ListBox is actually disappearing off the bottom of the Containing Control.
Does the ListBox actually stop properly or does it just seem to disappear? Try setting a MaxHeight on the ListBox and see if that makes the scrollbar appear. You should be able to set the VerticalScrollBarVisibility to Auto to have it only appear when needed.
If the list box is inside a StackPanel, try these steps for your ListBox
Set ScrollViewer.VerticalScrollBarVisibility="Auto"
Setting the Height property of a ListBox to some height that you expect to see.
That should force the scroll bar to show up.
This is pretty late, but anyone using ListBox probably shouldn't have it in a StackPanel. Once I switched the parent control of my Listbox from StackPanel to DockPanel with LastChildFill=True (Where the listbox was the last control), my scrollbar worked perfectly.
Hope this helps someone who's problem was not solved by the above answer.
Another solution to this problem that works well is to put a ScrollViewer around the StackPanel.
Another solution with a modification to Dave's is to use the ScrollViewer only. You only be able to scroll by placing your mouse on the ScrollView's ScrollBar. I use it this way because I don't like how ListBox jumps from item to item and sometimes missing items from the Top. Little bit hard on the eyes too. I like ScrollViewer's smooth scrolling.
I just ran into the same issue, and here's a little code demo on code project that visually shows it.
(If you want to save yourself the time of writing the code to see the differences yourself :) )
http://www.codeproject.com/Tips/659066/ListBox-and-Panels-in-WPF

Resources