Scrollviewbox 提供的空间比我想要的更多

发布于 2024-11-15 15:45:50 字数 1560 浏览 4 评论 0原文

我有一个这样定义的滚动视图框:

<ScrollViewer Width="150" Height="150" HorizontalScrollBarVisibility="Auto">
   <Grid Name="RootElement">
   </Grid>
</ScrollViewer>

在代码隐藏中,我用更多网格填充网格“RootElement”,

  void CreateGrid(uint _Columns, uint _Rows)
  {
     Grid layoutRoot = GetTemplateChild( "RootElement" ) as Grid;
     Random rand = new Random( );

     for(int r = 0; r < _Rows; r++)
     {
        layoutRoot.RowDefinitions.Add( new System.Windows.Controls.RowDefinition( ) { Height = new GridLength( 50, GridUnitType.Pixel ) } );
        for(int c = 0; c < _Columns; c++)
        {
           layoutRoot.ColumnDefinitions.Add( new System.Windows.Controls.ColumnDefinition( ) { Width = new GridLength( 50, GridUnitType.Pixel ) } );

           var border = new Border( );
           Grid.SetColumn( border, c );
           Grid.SetRow( border, r );

           Color col = new Color();
           col.A = (byte)rand.Next(255);
           col.R = (byte)rand.Next(255);
           col.G = (byte)rand.Next(255);
           col.B = (byte)rand.Next(255);
           border.Background = new SolidColorBrush( col );
           border.BorderBrush = new SolidColorBrush( Color.FromArgb( 0xff, 0x33, 0x33, 0x33 ) );

           layoutRoot.Children.Add( border );
        }
     }
  }

现在我的问题是,如果我在根网格内创建 10x10 网格(例如, CreateGrid(10, 10)) 我最终在滚动视图区域的右侧出现了大量的空白区域。随着我创建的网格单元数量的增加,空白区域似乎呈指数级增长。垂直方向上它很好,正常缩放完美,但水平方向上有很大的差距。也许只有 5% 的水平空间被网格占据。

我怎样才能使滚动查看器只覆盖其中网格的空间?

I have a scroll viewbox defined as such:

<ScrollViewer Width="150" Height="150" HorizontalScrollBarVisibility="Auto">
   <Grid Name="RootElement">
   </Grid>
</ScrollViewer>

And in codebehind I populate the grid 'RootElement' with even more grids,

  void CreateGrid(uint _Columns, uint _Rows)
  {
     Grid layoutRoot = GetTemplateChild( "RootElement" ) as Grid;
     Random rand = new Random( );

     for(int r = 0; r < _Rows; r++)
     {
        layoutRoot.RowDefinitions.Add( new System.Windows.Controls.RowDefinition( ) { Height = new GridLength( 50, GridUnitType.Pixel ) } );
        for(int c = 0; c < _Columns; c++)
        {
           layoutRoot.ColumnDefinitions.Add( new System.Windows.Controls.ColumnDefinition( ) { Width = new GridLength( 50, GridUnitType.Pixel ) } );

           var border = new Border( );
           Grid.SetColumn( border, c );
           Grid.SetRow( border, r );

           Color col = new Color();
           col.A = (byte)rand.Next(255);
           col.R = (byte)rand.Next(255);
           col.G = (byte)rand.Next(255);
           col.B = (byte)rand.Next(255);
           border.Background = new SolidColorBrush( col );
           border.BorderBrush = new SolidColorBrush( Color.FromArgb( 0xff, 0x33, 0x33, 0x33 ) );

           layoutRoot.Children.Add( border );
        }
     }
  }

Now my problem is if I create, say, 10x10 grids inside the root grid (e.g., CreateGrid(10,10)) I end up with a tonne of white space to the right of the scroll view area. The white space seems to increase exponentially as the number of grid cells I create increases. Vertically it's fine, scaled perfectly normally, but horizontally there's a massive gap. Maybe only 5% of the horizontal space is populated by the grids.

How can I make it so that the scrollviewer will only cover the space of the grids inside it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪心 2024-11-22 15:45:50

发生这种情况是因为您的 layoutRoot.ColumnDefinitions.Add() 位于内部循环内。对于 10 列和 10 行,您最终会为每 1 行创建 10 列,总共 100 列和 10 行。

分别迭代行和列以首先创建列/行定义。然后执行嵌套循环来创建控件。

for(int r = 0; r < _Rows; r++) {
    layoutRoot.RowDefinitions.Add( new System.Windows.Controls.RowDefinition( ) { Height = new GridLength( 50, GridUnitType.Pixel ) } );
}

for(int c = 0; c < _Columns; c++) {
    layoutRoot.ColumnDefinitions.Add( new System.Windows.Controls.ColumnDefinition( ) { Width = new GridLength( 50, GridUnitType.Pixel ) } );
}

for(int r = 0; r < _Rows; r++) {
    for(int c = 0; c < _Columns; c++) {
        var border = new Border( );
        ...
    }
}

That happens because you have your layoutRoot.ColumnDefinitions.Add() inside the inner loop. For 10 columns and 10 rows, you end up creating 10 columns for every 1 row, for a total of 100 columns and 10 rows.

Iterate over rows and columns separately to create the column/row definitions first. Then do the nested loops to create the controls.

for(int r = 0; r < _Rows; r++) {
    layoutRoot.RowDefinitions.Add( new System.Windows.Controls.RowDefinition( ) { Height = new GridLength( 50, GridUnitType.Pixel ) } );
}

for(int c = 0; c < _Columns; c++) {
    layoutRoot.ColumnDefinitions.Add( new System.Windows.Controls.ColumnDefinition( ) { Width = new GridLength( 50, GridUnitType.Pixel ) } );
}

for(int r = 0; r < _Rows; r++) {
    for(int c = 0; c < _Columns; c++) {
        var border = new Border( );
        ...
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文