Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I scroll a grid row in WPF/XAML?

Hi and thanks for looking!

Background

I have made a simple little app in WPF that has a grid layout consisting of one column and two rows. The top row holds a simple label for the header, and the bottom row holds a wrappanel that is dynamically populated with image thumbnails at runtime. Here is the XAML:

<Window x:Class="HTNavigator.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" WindowState="Maximized" WindowStyle="None">
    <Window.Background>
        <ImageBrush ImageSource="/HTNavigator;component/Images/HNBG.jpg" />
    </Window.Background>
    <Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="75"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>  
        <StackPanel Grid.Column="0" Grid.Row="0" Height="50" HorizontalAlignment="Left" Margin="30,10,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="Auto" FlowDirection="LeftToRight" Orientation="Horizontal" >
            <Label Content="Home Navigator v0.1" FontFamily="Tahoma" FontSize="18" FontWeight="Bold" Foreground="White" />
            <Button Content="Close" Height="50" Click="Button_Click"></Button>
        </StackPanel>
        <ScrollViewer Grid.Row="1" Name="MyScrollViewer" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
            <WrapPanel HorizontalAlignment="Center" Name="spContainer" VerticalAlignment="Top" ClipToBounds="True"></WrapPanel>
        </ScrollViewer>
    </Grid>
</Window>

Problem

The scrollbar does not show and mouse-wheel scrolling also does not work. I originally did not use the grid layout, and at that this time this portion of the XAML behaved as expected:

<ScrollViewer Name="MyScrollViewer" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
            <WrapPanel HorizontalAlignment="Center" Name="spContainer" VerticalAlignment="Top"               ClipToBounds="True" ItemHeight="Auto"> </WrapPanel>
</ScrollViewer>

Now everything lays out properly, but I don't get my vertical scroll ability (I do not want horizontal scroll).

Any thoughts?

Thanks!

Matt

like image 560
Matt Cashatt Avatar asked Jan 01 '12 00:01

Matt Cashatt


People also ask

How do you make a Grid scrollable in WPF?

How to enable scrollbar in a WPF TextBox. The simplest way to add scrolling functionality to a TextBox control is by enabling its horizontal and vertical scrolling. The HorizontalScrollBarVisibility and VerticalScrollBarVisibility properties are used to set horizontal and vertical scroll bars of a TextBox.

How do I add a ScrollBar to my Grid?

In the presentation tab of table layout properties, Choose width of content as Pixels(Fit Content) to see a horizontal scroll bar only for the grid with more columns.

How do I create a dynamic Grid in WPF?

The Grid class in WPF represents a Grid control. The following code snippet creates a Grid control, sets its width, horizontal alignment, vertical alignment, show grid lines, and background color. Grid DynamicGrid = new Grid();


1 Answers

I know it is New Years Eve but you have nothing in the WrapPanel and you have the row height to Auto so it will grow for content. Put something in the WrapPanel and set the height to *. And take the Column off the Label that is inside a StackPanel.

In your example of "before you had a Grid" there is a Grid.Row. Happy New Year.

like image 147
paparazzo Avatar answered Sep 19 '22 00:09

paparazzo