Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Scrollviewbox提供的空间比我想要的多_C#_Silverlight_Grid_Code Behind_Scrollviewer - Fatal编程技术网

C# Scrollviewbox提供的空间比我想要的多

C# Scrollviewbox提供的空间比我想要的多,c#,silverlight,grid,code-behind,scrollviewer,C#,Silverlight,Grid,Code Behind,Scrollviewer,我有一个滚动视图框,定义如下: <ScrollViewer Width="150" Height="150" HorizontalScrollBarVisibility="Auto"> <Grid Name="RootElement"> </Grid> </ScrollViewer> 在codebehind中,我用更多的网格填充网格“RootElement” void CreateGrid(uint _Columns, uin

我有一个滚动视图框,定义如下:

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

在codebehind中,我用更多的网格填充网格“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 );
        }
     }
  }
void CreateGrid(uint\u列,uint\u行)
{
Grid layoutRoot=GetTemplateChild(“根元素”)作为网格;
Random rand=新的Random();
对于(int r=0;r<_行;r++)
{
layoutRoot.RowDefinitions.Add(new System.Windows.Controls.RowDefinition(){Height=newgridlength(50,GridUnitType.Pixel)});
对于(int c=0;c<\u列;c++)
{
layoutRoot.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition(){Width=new GridLength(50,GridUnitType.Pixel)});
var border=newborder();
Grid.SetColumn(边框,c);
网格。SetRow(边框,r);
颜色col=新颜色();
列A=(字节)rand.Next(255);
列R=(字节)rand.Next(255);
G列=(字节)rand.Next(255);
col.B=(字节)rand.Next(255);
border.Background=新的SolidColorBrush(col);
border.BorderBrush=新的SolidColorBrush(Color.FromArgb(0xff,0x33,0x33));
layoutRoot.Children.Add(边框);
}
}
}
现在我的问题是,如果我在根网格内创建10x10个网格(例如,
CreateGrid(10,10)
),我最终会在滚动视图区域的右侧留下一吨空白。随着我创建的网格单元数量的增加,空白区域似乎呈指数增长。垂直方向很好,比例正常,但水平方向有很大的差距。可能只有5%的水平空间由网格填充


如何使scrollviewer只覆盖其中的网格空间?

这是因为您的
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( );
        ...
    }
}
for(int r=0;r<\u行;r++){
layoutRoot.RowDefinitions.Add(new System.Windows.Controls.RowDefinition(){Height=newgridlength(50,GridUnitType.Pixel)});
}
对于(int c=0;c<\u列;c++){
layoutRoot.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition(){Width=new GridLength(50,GridUnitType.Pixel)});
}
对于(int r=0;r<_行;r++){
对于(int c=0;c<\u列;c++){
var border=newborder();
...
}
}

这正是我的问题,我想我忽略了显而易见的问题。谢谢你,好心的先生!