是的,只需关闭 AutoGenerateColumns 并手动指定它们
在 MainWindow.xaml
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Hello}" Header="Hello" />
<DataGridTextColumn Binding="{Binding World}" Header="World" />
</DataGrid.Columns>
</DataGrid>
在 MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new[] { new FakeViewModel() };
}
}
在 FakeViewModel.cs
namespace WpfApplication4
{
class FakeViewModel
{
public FakeViewModel()
{
Hello = "Hello";
World = "World";
Then = DateTime.Now;
}
public DateTime Then { get; set; }
public string Hello { get; set; }
public string World { get; set; }
}
}
请注意未使用的属性然后!