0
votes

I am using silverlight and have to code in c# not xaml and i have a grid (myGrid) with 1 row and 1 column. This Grid further contain Border(rect) and this rect contains another grid (childGrid)inside having 1 row and 3 columns.

And this smallGrid further contains a stackpanel (sp) in second column whose size formed dynamically but smallGrid dont resize according to the growing size of stackpanel sp.

My code is like this:

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

The size of myGrid and childGrid must grow dynamically according to the increasing stackpanel size on dynamically created radio buttons? Because all the radio buttons are not displayed in column 2 please see the last radiobutton in snapshot belowis not displayed because of border crossing ("Very High", just after "High"): enter image description here

EDIT: I even tried these 3 steps but it still the same: (1)Remove rect.Width = g.Width; rect.Height = g.Height; childGrid.Height = sp.Height;

(2)add rowdef.Height= new GridLength(1, GridUnitType.Auto);(because i am expanding vertically)

(3) add sp.Orientation = Orientation.Vertical;

Edit2 : myGrid is liek this:

            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: (my changed code after bit's suggestion)

  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

1 Answers

1
votes

There a bunch of things you need to consider here..

In case you want a Horizontally expanding smallGrid, set the

sp.Orientation=Orientation.Horizontal

Then set the widths of the columns in smallGrid to Auto.

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

You don't really need the myGrid, directly using the Border should suffice.

Get rid of assigning the Height s eg:

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

Else, if you wanted a vertically expanding smallGrid, make sure the parent of the Border rect (or myGrid, in case you decide to keep it), allows for expansion Height (mostly check that height is not hard coded to a fix number or something)

Also, set the

smallGrid.ShowGridLines = true;

to give you a better idea in regards to what is actually taking up the space.

Lastly, do this too

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

}