using Microsoft.Win32; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Newtonsoft.Json; using System.IO; namespace WpfApp95 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.WindowState = WindowState.Maximized; InitGrid(); } DataGrid dg { get; set; } private void InitGrid() { Grid gd = new Grid(); RowDefinition row1=new RowDefinition(); RowDefinition row2=new RowDefinition(); row2.Height = new GridLength(50); gd.RowDefinitions.Add(row1); gd.RowDefinitions.Add(row2); ColumnDefinition column1 = new ColumnDefinition(); ColumnDefinition column2 = new ColumnDefinition(); column2.Width = new GridLength(150); gd.ColumnDefinitions.Add(column1); gd.ColumnDefinitions.Add(column2); gd.ShowGridLines= true; dg = new DataGrid(); Grid.SetRow(dg, 0); Grid.SetRowSpan(dg, 2); Grid.SetColumn(dg, 0); dg.Name = "dg"; Button btn = new Button(); btn.Content = "Load Data"; Grid.SetRow(btn,1); Grid.SetColumn(btn,1); btn.Click += Btn_Click; gd.Children.Add(btn); gd.Children.Add(dg); this.Content = gd; } private void Btn_Click(object sender, RoutedEventArgs e) { List<Book> booksList = new List<Book>(); for (int i = 0; i < 10000; i++) { Book bk = new Book(); bk.Id = i + 1; bk.ISBN = $"ISBN_{i + 1}"; bk.Name = $"Name_{i + 1}"; bk.Summary = $"Summary_{i + 1}"; bk.Title = $"Title_{i + 1}"; bk.Topic = $"Topic_{i + 1}"; booksList.Add(bk); } dg.ItemsSource = booksList; } //private void Button_Click(object sender, RoutedEventArgs e) //{ // List<Book> booksList = new List<Book>(); // for(int i=0;i<100;i++) // { // Book bk = new Book(); // bk.Id = i + 1; // bk.ISBN = $"ISBN_{i + 1}"; // bk.Name = $"Name_{i + 1}"; // bk.Summary = $"Summary_{i + 1}"; // bk.Title = $"Title_{i + 1}"; // bk.Topic = $"Topic_{i + 1}"; // booksList.Add(bk); // } // SaveFileDialog dlg = new SaveFileDialog(); // dlg.FileName = $"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}Books.json"; // if (dlg.ShowDialog() == true) // { // string jsonStr = JsonConvert.SerializeObject(booksList, Formatting.Indented); // File.WriteAllText(dlg.FileName, jsonStr); // } //} } public class Book { public int Id { get; set; } public string ISBN { get; set; } public string Name { get; set; } public string Summary { get; set; } public string Title { get; set; } public string Topic { get; set; } } }
标签:manually,C#,dg,Button,System,gd,Grid,new,using From: https://www.cnblogs.com/Fred1987/p/18175525