using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Dynamic; using System.Windows; using System.Windows.Controls; using System.Windows.Data; namespace WpfApp1 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { ObservableCollection<ExpandoObject> items = new ObservableCollection<ExpandoObject>(); public MainWindow() { InitializeComponent(); InitData(); } /// <summary> /// 初始化 /// </summary> public void InitData() { for (int i = 0; i < 5; i++) { dynamic item = new ExpandoObject(); item.Aa = "Property A value - " + i.ToString(); item.Bb = "Property B value - " + i.ToString(); items.Add(item); } dataGrid.Columns.Add(new DataGridTextColumn() { Header = "Aa", Binding = new Binding("Aa") }); dataGrid.Columns.Add(new DataGridTextColumn() { Header = "Bb", Binding = new Binding("Bb") }); dataGrid.ItemsSource = items; } /// <summary> /// 增加行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddData_Click(object sender, RoutedEventArgs e) { dynamic item = new ExpandoObject(); item.Aa = "New Item - A"; item.Bb = "New Item - B"; items.Add(item); } int newColumnIndex = 1; /// <summary> /// 增加列 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddColumn_Click(object sender, RoutedEventArgs e) { foreach (IDictionary<String, Object> item in items) { item.Add("NewColumn" + newColumnIndex, "New Column Value - " + newColumnIndex.ToString()); } dataGrid.Columns.Add(new DataGridTextColumn() { Header = "New Column" + newColumnIndex, Binding = new Binding("NewColumn" + newColumnIndex) }); newColumnIndex++; } private void showWindow_Click(object sender, RoutedEventArgs e) { Window1 window1 = new Window1(); window1.Show(); } /// <summary> /// 清除数据表 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ClearDataGrid_Click(object sender, RoutedEventArgs e) { //dataGrid.Items.Clear(); //dataGrid.ItemsSource = null; //dataGrid.Items.Refresh(); //dataGrid.Items.Clear(); dataGrid.ItemsSource = null; dataGrid.Items.Refresh(); dataGrid.Columns.Clear(); // } private void InitData_Click(object sender, RoutedEventArgs e) { items.Clear(); InitData(); } //private void Test() //{ // ThreadPool.QueueUserWorkItem(); // Delegate void WaitCallback(object state); //} } }
xaml
<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel Orientation="Horizontal"> <Button Content="Add Column" Click="AddColumn_Click" Margin="5"/> <Button Content="Add Data" Click="AddData_Click" Margin="5"/> <Button Content="Show Window" Click="showWindow_Click" Margin="5"></Button> <Button Content="清除内容" Click="ClearDataGrid_Click" Margin="5"></Button> <Button Content="初始化" Click="InitData_Click" Margin="5"></Button> </StackPanel> <DataGrid x:Name="dataGrid" AutoGenerateColumns="True" Grid.Row="1"/> </Grid> </Window>