我有代码为列表中的每个对象创建一个按钮。当每个对象被创建时,它被赋予一个对应于行和列的名称(即name = row1col2)。每个按钮都是动态生成的,添加到网格中,然后设置行/列。稍后,我需要收集“选定”按钮数据,以便我可以对它们所代表的数据执行操作。当我尝试从按钮中获取控件数据时,除网格行/列数据外,一切正常。它对网格中所有选定的行和列都保持不变。LayoutGrid SetRow和SetColumn无法按预期工作
创建按钮:
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);
}
}
我知道这一定是小东西我失踪。任何人有任何想法?
它为每个按钮的行和列返回什么值? – 2011-05-26 15:11:39
Row = 0和Col = 3(有4个按钮正在处理中) – kereberos 2011-05-26 15:25:34