I am new to WPF and the ScrollViewer is frustrating me. Either I just don't "get" it, or it is a limited control.
Here are my Frustrations:
Bad Horizontal Scrolling The horizontal scroll bar is only visible at the bottom of the list (I have to scroll to the bottom to see it)
Bad Borders I have a ListBox in my ScrollViewer. When I start the bottom of the list has no border and when I scroll down, the top border (line) of the list box disappears. I can kind of understand this, but attempts to set BorderThickness or BorderBrush for the ScrollViewer result in no change (I wanted to use the ScrollViewer's border to keep a constatant box around the list contents, like most list boxes out in cyber world).
Bad Handling of Short Lists When the items in the list don't reach the bottom the ScrollViewer keeps the scroll bar there and just dithers it out. Why not free up some space and remove it?
Some of these may seem petty (and they are). But users expect a certain look and feel from their apps and WPF is making it hard to get this out of the box.
If you know a way to fix any of these I would love a response. If there is a better way to deal with scrolling than using the ScrollViewer that would aslo be welcome.
Maybe you see some scroll bar from inside the list rather than the scroll bar from the ScrollViewer? Try setting <ScrollViewer ... HorizontalScrollBarVisibility="Auto"> (default is Hidden, which means that no horizontal scroll bar is shown ever; also try "Visible" for the sake of debugging)
Is putting a <Border> around the ScrollViewer an option?
VerticalScrollBarVisibility has a default value of Visible. If you want the scroll bar to disappear when it is not necessary, try <ScrollViewer ... VerticalScrollBarVisibility="Auto">.
Related
Situation:
I have a vertically orientated stack panel inside a ScrollViewer. The stack panel is configured to horizontally center-align its children. The scroll bar always appears at the right edge of the stack panel, which is what I want.
Then I fill the stack panel with children (user controls) of fixed widths, but all children are less wide than the stack panel. This leaves "empty space" to the left and right of the children. Visually, this does not matter, because background color is the same (which is nice).
But: In order to vertically scroll the stack panel using the mouse wheel, the mouse cursor seemingly must be positioned on top of one of the children. If the mouse cursor is positioned too much to the left or to the right (=over "empty space" in the stack panel) scrolling does not work!
Question:
How can I ensure that scrolling always works, no matter where I position the mouse cursor over the stack panel?
Martin.
Set a background on your StackPanel, any background will do. This was a known bug and this seems to fix the issue.
<StackPanel Background="White"/>
If this doesn't work, it might help to post your XAML code as it could be due to the way you have things wrapped.
I am trying to do something a little bit specific, and I can't seem to find if it is even possible. So I thought I would lay down the question here.
What I'm looking for is a way to change the anchor point of the scrollviewer from the top to the bottom (however you look at it). I'll explain what I mean.
By default when the control contained in your scrollviewer gets taller, so will your vertical scrollbar scollableheight.
But what happens is, the vertical scrollbar will stay at the same scrolloffset from the top, while what I want is that the vertical scrollbar keeps it's position relative to the bottom. The reason I want this behaviour is because I am creating a list that the user can expand more of by scrolling to the top. but then when I add the new items it looks like the scrollviewer scrolls up to the newly added items. (although it is actually the content the is pushed down) So if the scrollviewer would have the vertical scrollbar anchored to the bottom, the visible part of the content would stays at the same position to the user because the distance from that part to the bottom didn't change.
I hope you can understand what I'm trying to do.
I allready found one way of doing this, but it is not the desired option to me.
Option 1) Apply a Scaletransform of -1 in the Y-axis to the scrollviewer to flip it upside down. Then apply another ScaleTransform of -1 in the Y-axis to the content of the scrollviewer to make the content right side up.
But the problem with this technique is that for one, when you scroll using your mousewheel the scrollviewer scrolls inverted as normal. And second, I'm using 2 scaletransformations just to change the behaviour of the scrollviewer, and not even for eye candy. So that seems a bit excessive to me.
Option 2) anyone?
I'm working on a Windows Store App.
I have a ListView with a bunch of items in it. It is nested in some stack panels and grids, but they all essentially are auto sized. Essentially, the ListView is given most of the right half of the screen, regardless of what size it is.
If I give the ListView a hard-valued Height, a scrollbar will automatically appear with no extra work. Great. But I don't want to set a height... I want it to be all of the area available in its container. If I try to be clever and set it to 9999 or something then it will not scroll.
I've done a bunch of research, and similar questions like this say that the thing that holds the ListView cannot give it infinite size like a StackPanel would. What alternatives do I have? How can I put a ListView in something with arbitrary space and get a scroll bar to appear?
My only thought is that there must be some way to, in the container that holds the ListView, tell the ListView that it has all available area in such a way that it's height gets set that that value. Sort of like how to have to set ListViewItem's HorizontalContentAlignment property to 'Stretch' in order to get the items within a ListView to know what what available width they actually have.
The essential bits of my layout is this:
<!-- Nested in some other stuff simple Grids and StackPanels, none of which has hard heights set (all auto or *) -->
<!-- Even if I made this a Grid with one Row, setting definition to * or Auto doesn't help the issue - no scroll bar appears -->
<StackPanel>
<!-- Other stuff that has Visibility="Collapsed"... I have code so that only one item at a time within this container will ever be visible, and it gets all available space. -->
<ListView ItemsSource="{Binding SomeBigList}" ItemTemplate="{StaticResource MyDataTemplate}" />
<!-- Other stuff that has Visibility="Collapsed" -->
</StackPanel>
How can I get a scrollbar to appear in the ListView without setting a hard height anywhere? Thanks for you're help.
Don't use a <StackPanel>.
If you are in control over what gets drawn and only one item is ever visible at any one time, then use a non-infinite sized container like <Grid>. You can put each item in the same row, or use a separate row if only for ease of spotting what's what.
I ran into similar on the Windows Phone app. You need to remove the variables of the other containers by creating a test page and determining which panel is causing your ultimate problem.
Create new blank page with a Grid and ListView.
Work the grid sizing to be automatic (auto) or * sized until you see the horizontal scroll bar.
Once the horizontal scroll bar appears, add the other containers, one by one until the scroll bar disappears. That will inform you of the culprit which is causing the miss-sizing and ultimately the failure of the scrollbar to appear.
Research
Currently when working with WPF/XAML, the default scrolling behaviour for the scroll bar is that the VerticalScrollBarVisibility = Visible and the HorizontalScrollBarVisibility = Auto. This means the vertical scroll bar is always visible while the horizontal scroll bar is only visible is the content extends outside the width of the allocated area. Note that while the scrollbar is Visible, if the content do not extend beyong their allocated space, the scroll is deactivated and do not take any user input.
You may change this by changing the values to Visible, Auto, Hidden, or Disabled:
<ScrollViewer VerticalScrollBarVisibility="Visible"
HorizontalScrollBarVisibility = "Auto">
...
</ScrollViewer>
Problem
The problem is if you resize the window of the application and make it smaller, the scroll bars do not react as it should. It seems to just get cut off at the edges as I make the window smaller and smaller. The horizontal scrollbar is never activated. I then made the horizontal scrollbar visible just to see what happens. But it never changes to its active form when I make the windows smaller.
Can I assume that the scrollbar only activates when the content of the allocated space becomes to large for the space and not when the space is reduced when resizing the application window? If so does anyone know a way around this?
Edit
So I was working with my code and I realize that the scroll bar for the main window works as it should when the window is resized but that is not what I want. My main application window consists of three dock panels. Each of these panels needs to have its own scrollbars. It seems that when i resize the main window, the main window's scrollbars activate as appropriate but not the individual section's scrollbars. What I want to do is disable the main windows scroll bar altogether and have only the individual section's scrollbar work.
I've got a Silverlight user control that contains a datagrid. The datagrid is bound to an ObservableCollection. The user control is displayed in a popup window that I show in response to some event in another control
There are about 80 objects in the collection, and I've set a fixed height of 400 on the datagrid, so only about 20 items can be shown at one time, and the vertical scrollbar should be shown, with a thumb size occupying about 1/4th of the control height.
However, what's actually happening is that the scrollbar occupies almost the whole control height; maybe 95% of it. The datagrid displays the first 20 items. When I click on the scrollbar down arrow, the scrollbar moves down a little to occupy the bottom 95% of the scroll area, and the datagrid displays the last 20 items.
It gets weirder. If I move over the items in the grid and scroll down using the mouse wheel, then the items scroll correctly and I can view the whole list, although the scrollbar remains the same size.
And then, if I close the popup and reopen it, the scrollbar now appears with the correct height and it works correctly.
Finally, if I just show the control directly on the page, and not in a popup, it works fine.
What's going on here?! Has anyone else run into this problem? Help!
Update: The problem seems to be the way that I'm showing the popup. For various reasons, I had to implement the popup display in a somewhat tricky way, and it's this that's causing the trouble.
I recoded this, removing some crufty stuff, and the problem went away; see my update. This isn't really a solution as such, but I wanted to close out the question for the record.