Adding extra scrolling space into DataGrid - wpf

How to add an extra vertical scrolling space into the regular WPF DataGrid to provide an ability to scroll the last rows closer to the middle of the screen?
I've searched for the solution but the only suggestion I found is "adding empty rows to the end of the list".

The solution was pretty easy by using external ScrollViewer:
<ScrollViewer HorizontalScrollBarVisibility="Hidden">
<StackPanel>
<DataGrid ItemSource={Binding MyCollection}/>
<Rectangle Height="{Binding BottomSpace}"/>
</StackPanel>
</ScrollViewer>

Related

Content scrolling with DockPanel

I'm facing problem with scrolling content in Dock panel .
My controls placed in 'DockPanel' as below
<DockPanel>
<ScrollViewer>
<StackPanel>
<!-- Here controls are like Radiobutton,Lable ,CheckBox,Textblock are added dynamically in grid.-->
</StackPanel>
</ScrollViewer>
I'm using only vertical scrollbar, not need horizontal scrollbar,
When I first time traverse through controls in 'DockPanel' by using tab ,Tab focus goes off the screen but panel is not scrolling down.
Please help me out I'm really stuck over here.
Thanks in advance.
Add IsTabStop="True" in Scrollviewer:
<DockPanel>
<ScrollViewer IsTabStop="True" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<StackPanel></StackPanel>
</ScrollViewer>
</DockPanel>

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.

wpf datagrid footer layout

My footer is currently composed of a label and an ItemsControl. It needs to be below 2 DataGrid controls that are similar in that they both contain a fixed width column for every day in the week and the end of it (which is what the ItemsControl in the footer has totals for).
I'm trying to solve the alignment, which seems easiest to just right align the day columns. I thought I could use a DockPanel as a container and Dock=Right on the ItemsControl like below, but everything is starting on the left after the label.
<DockPanel x:Name="columnTotals" DockPanel.Dock="Bottom" >
<Label ... DockPanel.Dock="Left" Width="Auto">Grand Totals</Label>
<ItemsControl DockPanel.Dock="Right"
ItemsSource="{Binding Path=TotalTimeViewModels, Mode=OneWay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DockPanel>
So my primary question in this post is: why doesn't the ItemsControl actually dock to the right of it's parent container (currently also a DockPanel)?
Cheers,
Berryl
The secondary and more complicated question is how to make a footer line up with DataGrid columns. My main thought to date has been to use the fact that the last columns are always the same and their width is fixed.
I can make things line up with a StackPanel using FlowDirection=RightToLeft and 8 children labels (as opposed to one ItemsControl), but this has the disadvantages of making the XAML bloated and requires the text to be RightToLeft to reverse the container flow - it's messy and confusing to look at.
I guess the footer also could be another DataGrid, but the columns wouldn't be the same so I'd still have to solve the alignment by either working right to left as I'm doing now, or figure out what the starting point of the day columns is both initially and after a resize (I do not know how to do that).
Again, thanks for reading this and Cheers,
Berryl
This code solves the alignment problem using a DockPanel:
<DockPanel LastChildFill="True" DockPanel.Dock="Bottom" >
<ItemsControl DockPanel.Dock="Right" Width="Auto" ... />
<Label ... Width="Auto">Grand Totals</Label>
</DockPanel>
Cheers,
Berryl

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