如何将滚动条添加到网格

How to add Scrollbars to 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>

两个 canvases 可能太高或太宽。

这是 Tile Map Editor,我在 canvas 上绘制所有内容。在 ListView 我有要插入的图块。

通常你可以用 <ScrollViewer> 包裹元素,或者在元素的 XAML 中设置 ScrollViewer.HorizontalScrollBarVisibilityScrollViewer.VerticalScrollBarVisibility。我喜欢设置为 Auto,以便它们仅在需要时显示。

试试这个开始:

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

编辑以获取更多帮助!这是一个更好的布局示例,列表视图位于左侧,后面是两个画布。您可能希望将它们放在彼此之上或采用不同的布局,但这将向您展示如何操作:

<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>