WPF DataGrid 动态列绑定
代码:
<Grid>
<DataGrid Name ="dataGrid" HeadersVisibility="Column"
ItemsSource="{Binding}" DisplayMemberPath="Data"
CanUserAddRows =" False"
AutoGenerateColumns="False" />
</Grid>
public partial class MainWindow : Window
{
private ObservableCollection<string[]> _obervableCollection = new ObservableCollection<string[]>();
public MainWindow()
{
InitializeComponent();
addColumns();
addRow();
}
void addColumns()
{
for (int i = 0; i < 3; i++)
{
this.dataGrid.Columns.Add(new DataGridTextColumn() { Header = "Row" + i, Binding = new Binding("[" + i.ToString() + "]") });
}
dataGrid.IsReadOnly = true;
dataGrid.ItemsSource = _ober