Greggy
Greggy

Reputation: 719

How to add Scrollbars to Grid

How does one add scrollbars to a grid?

    <Grid>
        <Menu Height="23" Name="menu1" VerticalAlignment="Top">
            <MenuItem Header="File">
                <MenuItem Command="ApplicationCommands.New" Header="New" />
                <MenuItem Command="ApplicationCommands.Save" Header="Save" />
                <MenuItem Command="ApplicationCommands.Open" Header="Open" />
                <MenuItem Command="ApplicationCommands.Close" Header="Exit" />
            </MenuItem>
            <MenuItem Header="Stuff">
                <MenuItem Header="Properties" Command="Properties"/>
                <MenuItem Header="Tileset" Command="Replace"/>
            </MenuItem>
        </Menu>
        <Grid Margin="0,24,0,0">
            <Canvas HorizontalAlignment="Stretch" Name="canvas1" VerticalAlignment="Stretch" MouseMove="MoveMouse" MouseDown="PressDownMouse" MouseUp="canvas2_MouseLeftButtonUp" MouseWheel="canvas1_MouseWheel"/>
            <Canvas HorizontalAlignment="Stretch" Name="canvas2" VerticalAlignment="Stretch" MouseMove="MoveMouse" MouseDown="PressDownMouse" MouseUp="canvas2_MouseLeftButtonUp" MouseWheel="canvas1_MouseWheel"/>
            <ListView HorizontalAlignment="Left" Name="listView1" Width="203" VerticalAlignment="Stretch" SelectionChanged="listView1_SelectionChanged">
            </ListView>
        </Grid>
    </Grid>

Two canvases may be too high or too wide.

This is Tile Map Editor and I draw everything on canvas. In the ListView I have tiles to insert.

Upvotes: 20

Views: 35390

Answers (1)

Usually you can wrap the element with <ScrollViewer> or set ScrollViewer.HorizontalScrollBarVisibility and ScrollViewer.VerticalScrollBarVisibility inside the element's XAML. I like setting to Auto, so that they show up only when needed.

Just try this to start:

<ScrollViewer>
  <Grid>
  // some code
  </Grid>
</ScrollViewer>

EDIT for further help! Here's an example of a better layout, the listview is on the left followed by the two canvases. You may want to put these above each other or have a different layout, but this will show you how to do it:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Menu Name="menu1" >
        <MenuItem Header="File">
            <MenuItem Command="ApplicationCommands.New" Header="New" />
            <MenuItem Command="ApplicationCommands.Save" Header="Save" />
            <MenuItem Command="ApplicationCommands.Open" Header="Open" />
            <MenuItem Command="ApplicationCommands.Close" Header="Exit" />
        </MenuItem>
        <MenuItem Header="Stuff">
            <MenuItem Header="Properties" Command="Properties"/>
            <MenuItem Header="Tileset" Command="Replace"/>
        </MenuItem>
    </Menu>
    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ListView />
        <Canvas Grid.Column="1"/>
        <Canvas Grid.Column="2"/>
    </Grid>
</Grid>

Upvotes: 20

Related Questions