如何将Scrollbars添加到Grid

时间:2015-03-30 19:44:23

标签: c# wpf xaml scrollbar

如何将滚动条添加到网格?

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

两幅画布可能太高或太宽。

这是Tile Map Editor,我在画布上画了一切。在ListView我有要插入的图块。

1 个答案:

答案 0 :(得分:13)

通常,您可以使用<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>