Created
September 4, 2018 10:41
-
-
Save lazzyms/5c6c990acf205c06f474b97c219f8b20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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