在运行时,将X个带有SelectedItem的组合框添加到DataGrid(WPF) [英] At Runtime Add X number of ComboBoxes with SelectedItem to DataGrid (WPF)
问题描述
我想在 DataGrid
中创建整行的 ComboBoxes
。我在以下方面取得了一些进展:
I would like to create an entire row of ComboBoxes
in a DataGrid
. I have made some progress with the following:
// 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" />
是否可以设置默认的 SelectedValue
ComboBox 中的>?在我的 for
循环中,我确实可以访问所需的设置。另外,有没有一种方法可以显示它而不添加额外的列呢?此DataGrid与另一个没有该额外列的 DataGrid
对齐。
Is there a way to set the default SelectedValue
of each ComboBox
? Within my for
loop I do have access to the desired setting. Also, is there a way to git it to show without adding the extra column? This DataGrid is being lined up with another DataGrid
that won't have that extra column.
推荐答案
在解决了很多问题之后,我最终得出一个结论,即我试图做的事情要么是不可能的,要么是不合理的。我改为使用水平方向的 StackPanel
。以下是相关的摘要:
After a lot of working around the issue, I eventually came to the conclusion that what I was trying to do was either not possible, or unreasonable. I changed to using a StackPanel
with horizontal orientation. Here are the relevant snippets:
//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;
}
这篇关于在运行时,将X个带有SelectedItem的组合框添加到DataGrid(WPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!