I need to know how can I know when a ListView is vertical scrolling, to adjust some alignment in other components.
Something like this:
<ListView x:Name="lvList"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding ... , Mode=OneWay}"
SelectedItem="{Binding ...}">
<TextBlock Text="..."
HorizontalAlignment="{Binding ComputedVerticalScrollBarVisibility, ElementName=lvList.ScrollViewer ,Converter={StaticResource AlightByScroll}}"/>
I have tried putting the ListView inside ScrollViewer and it works, but then I have another problem with scrollViewer and touch scrolling. So I need to solve my problem knowing when the vertical scroll bar is visible in my ListView.
Related
I have a little problem. I want to put a view inside a stackPanel with a scrollbar. Basically, this is how I try to do this :
<DataTemplate x:Key="FirstTemplate">
<vw:FirstView DataContext="{Binding}"></vw:FirstView>
</DataTemplate>
<DataTemplate x:Key="SecondTemplate">
<vw:SecondView DataContext="{Binding}"></vw:SecondView>
</DataTemplate>
<DataTemplate x:Key="ThirdTemplate">
<vw:ThirdView DataContext="{Binding}"></vw:ThirdView>
</DataTemplate>
<selector:DimensionTemplateSelector x:Key="SomeTemplateSelector"
FirstTemplate="{StaticResource FirstTemplate}"
SecondTemplate="{StaticResource SecondTemplate}"
ThirdTemplate="{StaticResource ThirdTemplate}">
</selector:DimensionTemplateSelector>
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<StackPanel>
<ContentControl ContentTemplateSelector="{StaticResource SomeTemplateSelector}"
Content="{Binding ASelectedValue}" />
</StackPanel>
</ScrollViewer>
The problem with this code is that I can scroll with the mouse wheel only if my mouse is over the scrollbar zone. If my mouse is inside the stackPanel (so, inside the ScrollViewer tags) but not directly over the scrollbar zone, trying to scroll with the mouse wheel won't result in anything.
I'm very not sure, but it seem like I should define a behaviour to my contentControl so it can handle scrolling. Anyone know how to do that or have a better explanation ?
Thank you
I have a listbox under stackpanel and vertical scrollbar is visible but the scroller is not coming. The horizontal works fine. I can scroll across the listbox but not vertically.
<ListBox Width="700" Height="205" x:Name="log_Enteries" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible">
</ListBox>
I know setting height and width is not wise but it was desperate try I guess.
It's in a StackPanel. A vertically-oriented StackPanel will give its children whatever height they ask for, even if it means they run off the screen. Hence, the vertical ScrollBar will never show. Choose the right panel and you'll see the ScrollBar.
Use MaxHeight of your ListBox instead
You can also set your MaxHeight to parent StackPanel to achive want you want somthing like this:
MaxHeight="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}, Path=Height}"
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>
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.
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