我正在开发一个演示应用,我尝试根据组合框中的选择设置网格大小。我遇到的问题是,例如,当做一个2x2网格时。它没有填补网格;只有四分之一。我已经尝试弄乱行和列的大小以及内部的控件,但我不能让它按照我想要的方式运行。我正在寻找的行为对行和列的划分空间过于均等;然后控制填充单元格。当我调整窗口大小时,我想要网格,并且每个单元格中的控件也会随窗口调整大小。
这是我的代码:
int row = 0;
int col = 0;
short cam = 0;
for (int i = 0; i < (_length * _length); i++)
{
RowDefinition rowDef = new RowDefinition();
ColumnDefinition colDef = new ColumnDefinition();
//rowDef.Height = GridLength.Auto;
//colDef.Width = GridLength.Auto;
//rowDef.Height = new GridLength((this.ActualHeight / length) - 7, GridUnitType.Star);
//colDef.Width = new GridLength((this.ActualWidth / length) - 1, GridUnitType.Star);
//var converter = new GridLengthConverter();
//rowDef.Height = (GridLength)converter.ConvertFromString("*");
//colDef.Width = (GridLength)converter.ConvertFromString("*");
Border border = new Border();
border.BorderBrush = Brushes.White;
border.BorderThickness = new Thickness(2);
//rowDef.MinHeight = (this.ActualHeight / _length) - 7;
//colDef.MinWidth = (this.ActualWidth / _length) - 1;
rendererGrid.RowDefinitions.Add(rowDef);
rendererGrid.ColumnDefinitions.Add(colDef);
AxCVClientControlLib.AxCVVideo axCVVideo = new AxCVClientControlLib.AxCVVideo();
_hosts.Add(new System.Windows.Forms.Integration.WindowsFormsHost());
_hosts[i].Child = axCVVideo;
_hosts[i].HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
_hosts[i].VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
border.Child = _hosts[i];
//hosts[i].Width = (this.ActualWidth / length) - 1;
//hosts[i].Height = (this.ActualHeight / length) - 7;
axCVVideo.CreateControl();
axCVVideo.Camera = Convert.ToInt16(camCombo.SelectedIndex);
axCVVideo.XResolution = 640;
axCVVideo.YResolution = 480;
//System.Drawing.Rectangle rect = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
//axCVVideo.XResolution = (int)rect.Height;
//axCVVideo.YResolution = (int)rect.Width;
if (axCVVideo.ConnectSync())
{
axCVVideo.Connect();
}
_axCVs.Add(axCVVideo);
if (col == _length)
{
col = 0;
row++;
}
Grid.SetRow(border, row);
Grid.SetColumn(border, col);
rendererGrid.Children.Add(border);
//Grid.SetRow(hosts[i], row);
//Grid.SetColumn(hosts[i], col);
//rendererGrid.Children.Add(hosts[i]);
col++;
答案 0 :(得分:0)
<Grid x:Name="someRandomGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
这将导致网格有两行和两列。行总是ActualHeight
的一半,列始终是ActualWidth
的一半。
如果您真的坚持,可以在代码隐藏中执行此操作:
grid.RowDefinitions.Add(new RowDefinition() {
Height = new GridLength(1, GridUnitType.Star)
});
等。该数字决定了网格大小的分割方式。如果一列是1 *而另一列是2 *,则第一列将获得宽度的三分之一,第二列将获得三分之二。如果它们都是2 *,则它们都会得到宽度的2/4(a.k.a。1/2)。它将所有数字和分数相加。在XAML中没有数字的星形意味着&#34; 1 *&#34;。如果添加十几个1 *行,您将获得十几行,每行将在运行时动态调整为整个网格的实际高度的1/12。
这不是很直观,但一旦你学会了它,它就足够了。