2

I want to have a vertical scrollbar on my grid, which seems straightforward enough but for some reason it just won't work. When I set VerticalScrollBarVisibility to visible it shows up but does nothing. When it is set to auto it does not show up at all.

I have read the advice on this website but it doesn't seem to work for me. I know that rows should be set to fixed height or * and I have a combination of both. I also tried setting the max height of the grid and the height of the scroll bar, as was suggested, but neither of those worked either.

This is how I have it set up (the grid is within a tab):

</TabItem.Header>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<Grid Name="CSGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="400"/>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="20"/>
        <RowDefinition Height="5"/>
        <RowDefinition Height="1"/>
        <RowDefinition Height="20"/>
        <RowDefinition Height="20"/>
        <RowDefinition Height="20"/>
        ...

I then have a large number of rows whose contents I set through C# code, if that makes a difference. All of the heights are set to 20. After that I also have a number of rectangles and textblocks in the grid, nothing that I would imagine would be problematic - unless they would make a difference somehow?

In the code I add text to the rows like this:

TextBlock hist1 = new TextBlock();
TextBlock hist2 = new TextBlock();
TextBlock hist3 = new TextBlock();
TextBlock hist4 = new TextBlock();
TextBlock hist5 = new TextBlock();

string[] allHist = File.ReadAllLines("MedicalHistory.txt");

hist1.Text = allHist[0];
hist2.Text = allHist[1];
hist3.Text = allHist[2];
hist4.Text = allHist[3];
hist5.Text = allHist[4];

CSGrid.Children.Add(hist1);
CSGrid.Children.Add(hist2);
CSGrid.Children.Add(hist3);
CSGrid.Children.Add(hist4);
CSGrid.Children.Add(hist5);

Grid.SetColumn(hist1, 0);
Grid.SetColumn(hist2, 0);
Grid.SetColumn(hist3, 0);
Grid.SetColumn(hist4, 0);
Grid.SetColumn(hist5, 0);

Grid.SetRow(hist1, 5);
Grid.SetRow(hist2, 6);
Grid.SetRow(hist3, 7);
Grid.SetRow(hist4, 8);
Grid.SetRow(hist5, 9);

Any help would be greatly appreciated

4
  • I don't see a content getting added into ScrollViewer. Try something like <ScrollViewer ....> <TextBlock Text="Somelongstring" /> </ScrollViewer> and see whether it appears or not. Commented Dec 7, 2013 at 17:27
  • I was told that I usually had to have a big size inside the scroll viewer then set the desired size for the actual scroll viewer Commented Dec 7, 2013 at 18:45
  • That was my bad, I've removed that second example. Thanks for the tips. Yes, that's what I read too. I do have a lot within the scroll bar but when I set the max height of the grid it gets cut off, but the scroll bar still doesn't show... Commented Dec 7, 2013 at 18:50
  • 2
    I then have a large number of rows whose contents I set through C# code - Wrong. Don't create or manipulate UI elements in procedural code in WPF. That's what XAML is for. - Create a proper ViewModel for this and use DataBinding instead of a hacky horrible winforms-like procedural approach. Commented Dec 7, 2013 at 22:26

3 Answers 3

6

Use ScrollViewer.CanContentScroll="True" in your Grid:

<ScrollViewer VerticalScrollBarVisibility="Auto">
<Grid Name="CSGrid" ScrollViewer.CanContentScroll="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="400"/>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="20"/>
        <RowDefinition Height="5"/>
        <RowDefinition Height="1"/>
        <RowDefinition Height="20"/>
        <RowDefinition Height="20"/>
        <RowDefinition Height="20"/>
        ...
Sign up to request clarification or add additional context in comments.

Comments

2

You should set RowDefinition Height property more than TabControlHeight.

I coded this for you and works fine:

<Window x:Class="TestApp13.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:TestApp13"
    Title ="Title" Height="600" Width="800">
<TabControl>
    <TabItem Header="Tab 1">
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="200"/>
                    <RowDefinition Height="200"/>
                    <RowDefinition Height="200"/>
                    <RowDefinition Height="200"/>
                    <RowDefinition Height="200"/>
                    <RowDefinition Height="2000"/>
                </Grid.RowDefinitions>
                <Button Width="100" Height="30" Grid.Row="0"/>
                <Button Width="100" Height="30" Grid.Row="1"/>
                <Button Width="100" Height="30" Grid.Row="2"/>
                <Button Width="100" Height="30" Grid.Row="3"/>
                <Button Width="100" Height="30" Grid.Row="4"/>
                <Button Width="100" Height="30" Grid.Row="5"/>
            </Grid>
        </ScrollViewer>
    </TabItem>
</TabControl>

4 Comments

Thanks, but that what I can't understand. Even when I set the height of all the rows, it still doesn't work. I'm really confused. Could it have anything to do with how I'm adding children to the grid? I'm just using CSGrid.Children.Add(), and it seems to work fine
Can you post xaml and cs file? Therefore i can solve your question.
Can you post overall xaml and cs file?
Ok I finally got it - it was the TabControl whose height I had to set, I was focusing on the grid the whole time (the grid and scrollviewer are within a tab): <TabControl Grid.ColumnSpan="5" Grid.RowSpan="15" Height="600">. Thanks for your help
0
<Window x:Class="TestApp13.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:TestApp13"
    Title ="Title" Height="600" Width="800">
<TabControl>
    <TabItem Header="Tab 1">
        <ScrollViewer Hight="500">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="200"/>
                    <RowDefinition Height="200"/>
                    <RowDefinition Height="200"/>
                    <RowDefinition Height="200"/>
                    <RowDefinition Height="200"/>
                    <RowDefinition Height="2000"/>
                </Grid.RowDefinitions>
                <Button Width="100" Height="30" Grid.Row="0"/>
                <Button Width="100" Height="30" Grid.Row="1"/>
                <Button Width="100" Height="30" Grid.Row="2"/>
                <Button Width="100" Height="30" Grid.Row="3"/>
                <Button Width="100" Height="30" Grid.Row="4"/>
                <Button Width="100" Height="30" Grid.Row="5"/>
            </Grid>
        </ScrollViewer>
    </TabItem>
</TabControl>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.