c#为什么stackpanel不调整大小?

时间:2014-05-26 20:44:40

标签: c# silverlight grid silverlight-5.0 stackpanel

我正在使用silverlight并且必须在c#中编码而不是xaml,并且我有一个网格(myGrid),包含1行和1列。此网格还包含边框(rect),此rect包含另一个网格(childGrid),内部包含1行和3列。

这个smallGrid在第二列中还包含一个stackpanel(sp),其大小是动态形成的,但smallGrid不会根据stackpanel sp的增长大小调整大小。

我的代码是这样的:

   Grid myGrid = new Grid();
   myGrid.Width = 750;
   myGrid.HorizontalAlignment = HorizontalAlignment.Left;
   myGrid.VerticalAlignment = VerticalAlignment.Top;
   myGrid.ShowGridLines = false;

   ColumnDefinition colDef1 = new ColumnDefinition();
   myGrid.ColumnDefinitions.Add(colDef1);
   RowDefinition rowDef1 = new RowDefinition();
   myGrid.RowDefinitions.Add(rowDef1);


   Border rect = new Border();
   rect.Width = g.Width;
   rect.Height = g.Height;
   rect.BorderThickness = new Thickness(2);
   rect.BorderBrush = new SolidColorBrush(Colors.LightGray);

   Grid childGrid = new Grid();
   ColumnDefinition colDef1 = new ColumnDefinition();
   ColumnDefinition colDef2 = new ColumnDefinition();
   ColumnDefinition colDef3 = new ColumnDefinition();
   childGrid.ColumnDefinitions.Add(colDef1);
   childGrid.ColumnDefinitions.Add(colDef2);
   childGrid.ColumnDefinitions.Add(colDef3);


   int NumberOfRadioButton = 0;
   StackPanel sp = new StackPanel();

   foreach(var item in param.Component.Attributes.Items) {
       NumberOfRadioButton++;
       RadioButton rb = new RadioButton();
       rb.GroupName = item;
       rb.Content = item;
       sp.Children.Add(rb);

   }
   Grid.SetRow(sp, 1);
   Grid.SetColumn(sp, 1);
   childGrid.Height = sp.Height;
   childGrid.Children.Add(sp);

   TextBlock txtblk1ShowStatus = new TextBlock();
   TextBlock txtblkLabel = new TextBlock();

   txtblkLabel.Text = param.Label;
   Grid.SetColumn(txtblkLabel, 0);
   Grid.SetRow(txtblkLabel, 1);
   childGrid.Children.Add(txtblkLabel);

   txtblk1ShowStatus.Text = param.Name;
   Grid.SetColumn(txtblk1ShowStatus, 2);
   Grid.SetRow(txtblk1ShowStatus, 1);
   childGrid.Children.Add(txtblk1ShowStatus);

   rect.Child = childGrid;
   Grid.SetRow(rect, 1);
   myGrid.Children.Add(rect);

myGridchildGrid的大小必须根据动态创建的单选按钮上不断增加的堆栈面板大小动态增长?因为第2列中没有显示所有单选按钮,所以请看下面快照中的最后一个单选按钮是否因为过境而未显示("非常高",在"高&#34之后;): enter image description here

编辑:我甚至尝试了这三个步骤,但它仍然是相同的: (1)删除rect.Width = g.Width; rect.Height = g.Height; childGrid.Height = sp.Height;

(2)添加rowdef.Height = new GridLength(1,GridUnitType.Auto);(因为我正在垂直扩展)

(3)添加sp.Orientation = Orientation.Vertical;

Edit2:myGrid谎言:

            Grid myGrid = new Grid();
            myGrid.Width = 750;       
            myGrid.HorizontalAlignment = HorizontalAlignment.Left;
            myGrid.VerticalAlignment = VerticalAlignment.Top;
            myGrid.ShowGridLines = false;

            ColumnDefinition colDef1 = new ColumnDefinition();
            myGrid.ColumnDefinitions.Add(colDef1);       
            int totalRows = p.Parameter.Count() + p.Separator.Count();

            for (int i = 0; i < totalRows; i++)
            {
                myGrid.RowDefinitions.Add(new RowDefinition());

            }    

EDIT3 :(在我的建议之后我改变了代码)

  Border rect = new Border();
// rect.Width = g.Width;
// rect.Height = g.Height;
    rect.BorderThickness = new Thickness(2);
    rect.BorderBrush = new SolidColorBrush(Colors.LightGray);

    Grid childGrid = new Grid();
    ColumnDefinition colDef1 = new ColumnDefinition();
    ColumnDefinition colDef2 = new ColumnDefinition();
    ColumnDefinition colDef3 = new ColumnDefinition();
    RowDefinition rowdef = new RowDefinition();
    childGrid.ColumnDefinitions.Add(colDef1);
    childGrid.ColumnDefinitions.Add(colDef2);
    childGrid.ColumnDefinitions.Add(colDef3);

    childGrid.RowDefinitions.Add(rowdef);
    rowdef.Height= new GridLength(1, GridUnitType.Auto);

     int NumberOfRadioButton =0;
     StackPanel sp = new StackPanel();
     sp.Orientation = Orientation.Vertical;

     foreach (var item in param.Component.Attributes.Items)
     {                 
         NumberOfRadioButton++;
         RadioButton rb = new RadioButton();
         rb.GroupName = item;
         rb.Content = item;
         sp.Children.Add(rb);

     }
    Grid.SetRow(sp, LoopCount);
    Grid.SetColumn(sp, 1);
    childGrid.ShowGridLines = true;
 //   rect.Height = double.NaN;
    childGrid.Children.Add(sp);

    TextBlock txtblk1ShowStatus = new TextBlock();
    TextBlock txtblkLabel = new TextBlock();

    txtblkLabel.VerticalAlignment = System.Windows.VerticalAlignment.Center;
    txtblkLabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
    txtblkLabel.TextAlignment = System.Windows.TextAlignment.Center;
    txtblkLabel.FontWeight = FontWeights.Bold;
    txtblkLabel.FontSize = 15;
    txtblkLabel.FontStyle = FontStyles.Normal;
    txtblkLabel.Padding = new Thickness(5, 10, 5, 10);

    txtblkLabel.Text = param.Label;
    Grid.SetColumn(txtblkLabel, 0);
    Grid.SetRow(txtblkLabel, LoopCount);
    childGrid.Children.Add(txtblkLabel);

    txtblk1ShowStatus.VerticalAlignment = System.Windows.VerticalAlignment.Center;
    txtblk1ShowStatus.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
    txtblk1ShowStatus.TextAlignment = System.Windows.TextAlignment.Center;
    txtblk1ShowStatus.FontWeight = FontWeights.Bold;
    txtblk1ShowStatus.FontSize = 15;
    txtblk1ShowStatus.FontStyle = FontStyles.Normal;
    txtblk1ShowStatus.Padding = new Thickness(5, 10, 5, 10);

    txtblk1ShowStatus.Text = param.Name;
    Grid.SetColumn(txtblk1ShowStatus, 2);
    Grid.SetRow(txtblk1ShowStatus, LoopCount);
    childGrid.Children.Add(txtblk1ShowStatus);

    rect.Child = childGrid;
    Grid.SetRow(rect, LoopCount);
    myGrid.Children.Add(rect);

1 个答案:

答案 0 :(得分:1)

这里有一些你需要考虑的事情..

如果您想要水平展开smallGrid,请设置

sp.Orientation=Orientation.Horizontal

然后将smallGrid中列的宽度设置为自动。

colDef.Width = new GridLength(1, GridUnitType.Auto); // Auto

您真的不需要myGrid,直接使用Border就足够了。

摆脱分配高度,例如:

rect.Width = g.Width;
rect.Height = g.Height;
childGrid.Height = sp.Height;

否则,如果你想要一个垂直扩展的smallGrid,请确保Border rect(或myGrid的父级,如果你决定保留它),允许扩展高度(大多数检查高度不是硬编码)修正数字或其他)

另外,设置

smallGrid.ShowGridLines = true;

让您更好地了解实际占用空间的内容。

最后,也这样做

int totalRows = p.Parameter.Count() + p.Separator.Count(); 

for (int i = 0; i < totalRows; i++) 
{ 
var rowDef=new RowDefinition(); 
rowdef.Height = new GridLength(1, GridUnitType.Auto); 
myGrid.RowDefinitions.Add(rowDef); 

}