【发布时间】:2018-11-15 05:31:30
【问题描述】:
我想在DataGrid 中创建一整行ComboBoxes。我在以下方面取得了一些进展:
// Declare it
private DataGridComboBoxColumn CreateCustomComboBoxDataSouce(string ColumnName)
{
string[] data = { "Date", "LEInt", "String" };
DataGridComboBoxColumn dgCmbCol = new DataGridComboBoxColumn();
dgCmbCol.Header = ColumnName;
dgCmbCol.ItemsSource = data;
return dgCmbCol;
}
// Later somewhere you can add this to create 20 columns:
for (int i = 0; i < 20; i++)
{
DataGridComboBoxColumn newColumn = CreateCustomComboBoxDataSouce("Column-" +i);
}
// Sadly nothing is shown unless you manually specify a new row and an
// extra column as done here below, but then you get an extra column.
DataTable table = new DataTable();
table.Columns.Add("|", typeof(string));
table.Rows.Add("");
DataGridCombo.DataContext = table;
XAML 保持在最低限度:
<DataGrid x:Name="DataGridCombo" ItemsSource="{Binding}" Margin="0,0,0,0" />
有没有办法设置每个ComboBox 的默认SelectedValue?在我的for 循环中,我确实可以访问所需的设置。另外,有没有办法让它显示而不添加额外的列?此 DataGrid 正在与另一个没有额外列的 DataGrid 对齐。
【问题讨论】: