Skip to content

Instantly share code, notes, and snippets.

@lazzyms
Created September 4, 2018 10:41
Show Gist options
  • Save lazzyms/5c6c990acf205c06f474b97c219f8b20 to your computer and use it in GitHub Desktop.
Save lazzyms/5c6c990acf205c06f474b97c219f8b20 to your computer and use it in GitHub Desktop.
DataGrid dg = new DataGrid();
dg.HorizontalAlignment = HorizontalAlignment.Left;
dg.VerticalAlignment = VerticalAlignment.Top;
dg.AutoGenerateColumns = true;
//the function getData used to get the data from data-source, you can manipulate it.
getData gd = new getData(); // getData holds getUserRecord() to get the data
UserData[] data = gd.getUserRecord();
dg.ItemsSource = data;
FrameworkElementFactory sp = new FrameworkElementFactory(typeof(StackPanel));
sp.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
FrameworkElementFactory delete = new FrameworkElementFactory(typeof(Button));
delete.AddHandler(Button.ClickEvent, new RoutedEventHandler((s, e) => { MessageBox.Show(data[dg.SelectedIndex].id); }));
delete.SetValue(ContentControl.ContentProperty, "Delete");
FrameworkElementFactory edit = new FrameworkElementFactory(typeof(System.Windows.Controls.Button));
edit.AddHandler(Button.ClickEvent, new RoutedEventHandler((s, e) => { MessageBox.Show("edited click!"); }));
edit.SetValue(ContentControl.ContentProperty, "Edit");
edit.SetValue(FrameworkElement.MarginProperty, new Thickness(5, 0, 0, 0));
sp.AppendChild(delete);
sp.AppendChild(edit);
DataGridTemplateColumn dataGridTemplateColumn = new DataGridTemplateColumn()
{
Header = "new...",
CellTemplate = new DataTemplate { VisualTree = sp }
};
dg.Columns.Add(dataGridTemplateColumn);
panel1.Children.Add(dg);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment