后台代码动态创建silverlight的Grid

Code below can add item that contain a button and a textbox which are lay in a grid to combobox.

ComboBoxItem item = new ComboBoxItem();
Grid grid = new Grid();
grid.ColumnDefinitions.Insert(0, new ColumnDefinition() { Width = new GridLength(50) });
grid.ColumnDefinitions.Insert(1, new ColumnDefinition() { Width = new GridLength(100) });

Button MyButton = new Button();
MyButton.SetValue(Grid.ColumnProperty, 0);
MyButton.DataContext = "11";
MyButton.Click+=new RoutedEventHandler(MyButton_Click);
TextBox textbox = new TextBox();
textbox.Name = "text";
textbox.SetValue(Grid.ColumnProperty, 1);
grid.Children.Add(MyButton);
grid.Children.Add(textbox);
item.Content = grid;
Combobox1.Items.Add(item);

你可能感兴趣的:(silverlight)