3
votes

I have a number of UserControls inside a ListView. Somehow WPF implemented horizontal scrolling even without a ScrollViewer. When I try to implement vertical scrolling, the ScrollViewer is grey and unusable. I tried wrapping the ListView in a ScrollViewer but it will not scroll. Even if I delete the ListView and only try to put a StackPanel with Textboxes in it, the ScrollViewer is still disabled. Is there something I'm missing?

XAML:

<ScrollViewer VerticalScrollBarVisibility="Visible" CanContentScroll="True" 
Grid.Row="1" Grid.Column="1">
            <ListBox Margin="0,0,10,10" ItemsSource="{Binding Feeder}" 
Grid.RowSpan="3">
             <ListBox.ItemContainerStyle>
                 <Style TargetType="{x:Type ListBoxItem}">
                     <Setter Property="Focusable" Value="False"/>
                 </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <local:FeederControl FeederName="{Binding FeederName}" 
AxisList="{Binding AxisList}"></local:FeederControl>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
</ScrollViewer>
2

2 Answers

2
votes

ListView/Listbox/Stackpanel : can expand based on the size of its content.

Wrap your Listview/listbox inside a Grid with height "*" (Don't use "Auto" for height property, it will make it expand like a stack panel, based on its contents)

1
votes

This works fine for me. Maybe you are not showing the rest of the code. This is what I made from your example and it works as expected...

XAML:

<Window x:Class="WPF_Playground.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">
    <Grid>
        <ScrollViewer VerticalScrollBarVisibility="Auto" CanContentScroll="True">
            <ListBox ItemsSource="{Binding Items}">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ListBoxItem}">
                        <Setter Property="Focusable" Value="False"/>
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Text}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </ScrollViewer>
    </Grid>
</Window>

ViewModel:

using System.Collections.Generic;
using System.Windows;

namespace WPF_Playground
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public IEnumerable<Item> Items => new Item[]
        {
            new Item{ Text = "Something" },
            new Item{ Text = "Something" },
            new Item{ Text = "Something" },
            new Item{ Text = "Something" },
            new Item{ Text = "Something" },
            new Item{ Text = "Something" },
            new Item{ Text = "Something" },
            new Item{ Text = "Something" },
            new Item{ Text = "Something" },
            new Item{ Text = "Something" },
            new Item{ Text = "Something" },
            new Item{ Text = "Something" },
            new Item{ Text = "Something" },
            new Item{ Text = "Something" },
            new Item{ Text = "Something" },
            new Item{ Text = "Something" }
        };

        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;
        }
    }  

    public class Item
    {
        public string Text { get; set; }
    }
}

If you resize the window the sroll bar will apear when the hosted control cannot display all the controls any more. Pretty much standard stuff.