I'm trying to create a DataGrid which has drop-down menus in the headers and I'm not sure how to go about doing so.
The DataGrid itself is populated from a CSV file which can have many different formats so I can't pre-define the columns in XAML, instead set them up like:
//add headers as columns on the data grid
var headerRow = _inputFile.GetHeaderRow();
foreach (var hr in headerRow)
{
var textColumn = new DataGridTextColumn();
textColumn.Header = hr;
SourceGrid.Columns.Add(textColumn);
}
My goal is to also have drop-down menus in the header column (or first row if suitably styled) as the idea is that I'm mapping the input CSV file to a set of pre-defined outputs, so I'd like the user to select per input file header what to map it to via the drop-down menu.
Is it better to attempt to define a data grid template in xaml? Or perhaps arrange my input data into a class structure which will more easily databind to the grid in the way I want? Or something else entirely?
I'm fairly new to WPF and having trouble finding where to go from here so even pointers in the right direction would very helpful!
Edit:
var comboColumn = new DataGridComboBoxColumn();
comboColumn.Header = new ComboBox();
SourceGrid.Columns.Add(comboColumn);
So I understand I can setup the headers to be text or combo boxes in code, but is there a way to put both a text and combobox in the same column header? Also, is my approach to solve this problem a good one or should I be looking at a different kind of control? Or perhaps using databinding instead of setting the values in code?
The below code will help you to display the Combobox header in wpf.
<DataGridTextColumn Binding="{Binding FirstName}">
<DataGridTextColumn.Header>
<ComboBox />
</DataGridTextColumn.Header>
</DataGridTextColumn>