在运行时,将X个带有SelectedItem的组合框添加到DataGrid(WPF)

时间:2018-11-13 20:01:41

标签: c# wpf datagrid

我想在NULL中创建ComboBoxes的整行。我在以下方面取得了一些进展:

DataGrid

XAML保持最小:

// 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;

是否可以设置每个<DataGrid x:Name="DataGridCombo" ItemsSource="{Binding}" Margin="0,0,0,0" /> 的默认SelectedValue?在我的ComboBox循环中,我可以访问所需的设置。另外,有没有一种方法可以显示它而不添加额外的列呢?该DataGrid正在与另一个for对齐,该DataGrid不会有多余的列。

2 个答案:

答案 0 :(得分:0)

  

是否可以设置每个SelectedValue的默认ComboBox

DataColumn的{​​{1}}中添加DataTable,然后将每一行的列值设置为所需的选定值:

DataGridComboBoxColumn
  

还可以在不添加额外列的情况下对其进行git显示吗?

const int n = 20; DataTable table = new DataTable(); for (int i = 0; i<n; i++) { string columnName = "Column-" + i; DataGridCombo.Columns.Add(CreateCustomComboBoxDataSouce(columnName)); table.Columns.Add(columnName, typeof(string)); } table.Rows.Add(new object[n]); //select some values... table.Rows[0][1] = "LEInt"; table.Rows[0][5] = "String"; DataGridCombo.DataContext = table; 属性设置为HorizontalAlignment,以防止Left水平拉伸:

DataGrid

答案 1 :(得分:0)

在围绕该问题进行了大量工作之后,我最终得出的结论是,我试图做的事情要么是不可能的,要么是不合理的。我改为使用水平方向的StackPanel。以下是相关片段:

//XAML Layout
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Name="ComboBoxPanel"  Grid.Row="1"  Margin="0,0,0,0"  HorizontalAlignment="Left" />

//Function to create new box and set options
private ComboBox CreateComboBox(string ColumnName, string selectedItem, int width)
{
    string[] data = { "Date", "LEInt", "String" };

    ComboBox dgCmbCol = new ComboBox();
    dgCmbCol.ItemsSource = data;
    dgCmbCol.Name = ColumnName;
    dgCmbCol.Width = width;
    dgCmbCol.SelectedItem = selectedItem;
    dgCmbCol.SelectionChanged += DataTypeSelectionChanged;
    return dgCmbCol;
}

// This reads a JSON file and uses it's options to set the width of all the columns
// for the DataGrid and matches the ComboBoxes to it so they all line up
dynamic TableData = JsonConvert.DeserializeObject(Config.DataLayoutDefinition);
ComboBoxPanel.Children.Clear(); //Since this is run on user interaction, clear old contents.

for (int i = 0; i <  DataGridView.Columns.Count; i++)
{
    int width = TableData.data[i].ColumnWidth;
    if (TableData.data[i].DataType == "String")
    {
        width = 125;
    }
    else if (TableData.data[i].DataType == "Date")
    {
        width = 150;
    }
    else if (TableData.data[i].DataType == "LEInt")
    {
        width = 80;
    }
    else
    {
        width = 100;
    }
ComboBoxPanel.Children.Add(CreateComboBox(TableData.data[i].Column.ToString(), TableData.data[i].DataType.ToString(), width));
    DataGridView.Columns[i].Width = width;

}
相关问题