LayoutGrid SetRow和SetColumn无法按预期工作

问题描述

| 我有为列表中的每个对象创建一个按钮的代码。创建每个对象时,将为其指定与行和列相对应的名称(即name = row1col2)。每个按钮都是动态生成的,添加到网格中,然后设置行/列。稍后,我需要收集“选定”按钮数据,以便可以对它们表示的数据执行操作。当我尝试从按钮获取控制数据时,除网格行/列数据外,其他一切都很好。对于网格中所有选定的行和列,该字段均保持不变。 创建按钮:
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);
}
取回按钮数据
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);
    }
}
我知道这一定是我所缺少的小东西。任何人有任何想法吗?     

解决方法

尽管第二部分代码中的注释显示:
//create a stack object
您实际上并没有创建一个新的堆栈对象,因此它只是在每次迭代中覆盖ѭ3的单个实例。最后看到的行和列的值是上次迭代的值。 最终结果是“ 4”是对象的所有相同实例的集合,而不是单独实例的集合。     ,这只是一个黑暗的镜头,但是基于这个问题和这个问题,可能是您需要在将按钮添加到其父项之前设置“行”和“列”属性-类似于:
//put the button into the grid
Grid.SetRow(stackButton,0);
Grid.SetColumn(stackButton,i-1);
newRow.Children.Add(stackButton); 
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...