110,536
社区成员
发帖
与我相关
我的任务
分享
<Window
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"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2" x:Class="WpfApp1.MainWindow"
mc:Ignorable="d"
Title="MainWindow" x:Name="mywindow" Height="450" Width="800">
<Grid>
<DataGrid x:Name="datagrid1" AutoGenerateColumns="False" Width="400" Height="200">
<DataGrid.Columns>
<DataGridTextColumn Header="姓名" Width="0" Binding="{Binding PersonName}" />
<DataGridTextColumn Header="年龄" Width="0" Binding="{Binding Age}" >
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<Button Content="Button" Width="75" Click="Button_Click" />
</Grid>
</Window>
private void Button_Click(object sender, RoutedEventArgs e)
{
datagrid1.ColumnWidth = DataGridLength.Auto;
ObservableCollection<Person> obc = new ObservableCollection<Person>();
obc.Add(new Person() { PersonName = "张三", Age = 43});
obc.Add(new Person() { PersonName = "李四", Age = 42});
obc.Add(new Person() { PersonName = "王娅", Age = 33});
obc.Add(new Person() { PersonName = "张五", Age = 50});
datagrid1.ItemsSource = obc;
}