1

I have code that creates a button for each object in a list. When each object is created it is given a name that corresponds to the row and column (i.e. name = row1col2). Each button is generated dynamically, added to the grid and then the row/column are set. At a later time I need to gather the "selected" button data so I can perform actions on the data they represent. When I attempt to get the control data from the buttons everything is fine, except for the grid row/column data. It remains the same for all of the selected rows and columns in a grid.

Creating buttons:

for (int i = 1; i < row.StackCount+1; i++)
{
    //create button for the column
    stackButton = new Button();
    stackButton.Height = ((newRow.Height - 2));
    stackButton.Width = ((newRow.Width / row.StackCount) - 2);
    stackButton.Background = new SolidColorBrush(Colors.White);

    //add the button border
    stackButton.BorderBrush = new SolidColorBrush(Colors.Black);
    stackButton.BorderThickness = new Thickness(1);
    stackButton.Style = Application.Current.Resources["flatButton"] as Style;

    //add the button name
    stackButton.Name = "Row" + row.LineNumber + "Col" + (i - 1).ToString();

    //add the event handler to the button
    stackButton.Click += new RoutedEventHandler(stackButton_Click);

    //add a new column
    newRow.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(newRow.Width, GridUnitType.Star) });

    //put the button into the grid
    newRow.Children.Add(stackButton); 
    Grid.SetRow(stackButton, 0);
    Grid.SetColumn(stackButton, i-1);
}

Getting the Button data back

g = (Grid)b.Child;
foreach (Button currentButton in g.Children)
{
    if (((SolidColorBrush)currentButton.Background).Color == Colors.Gray)
    {
        //create a stack object
        buttonData.StartDate = DateTime.Now;
        buttonData.LotNumber = LotDisplay.Text;
        buttonData.RoomID = SilverGlobals.CurrentRoom.RoomID;

        buttonData.RoomCol = Grid.GetColumn(currentButton);
        buttonData.RoomRow = Grid.GetRow(currentButton);

        buttonData.TrayCount = int.Parse(currentButton.Content.ToString());
        buttonData.Status = 0;

        //add stack object to list of stack objects
        stacks.Add(buttonData);
    }
}

I know this must be something small I am missing. Anyone got any ideas?

kereberos
  • 1,253
  • 1
  • 10
  • 20

2 Answers2

1

Although the comment in your second section of code says:

//create a stack object

you don't actually create a new stack object so it is simply overwriting the single instance of buttonData on each iteration. The values for row and column that you see at the end are the last iteration's values.

The net effect is that stacks is a collection of all the same instance of an object instead of a collection of separate instances.

Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95
0

This is just a shot in the dark, but based on this question and this question, it might be that you need to set the Row and Column properties before you add the button to its parent - something like this:

//put the button into the grid
Grid.SetRow(stackButton, 0);
Grid.SetColumn(stackButton, i-1);
newRow.Children.Add(stackButton); 
Community
  • 1
  • 1
E.Z. Hart
  • 5,717
  • 1
  • 30
  • 24