VS 2010, WPF project.
I have a datagrid with text in all cells (data are from an array of custom objects). However I am unable to copy (ctrl+c) content of any cell. So I would like to use textboxes to display content of each cell. The real problem is I cannot do this in XAML but I have to do it dynamically in the code, because entire grid is build manually in code, so XAML only knows there is a grid.
I add the columns this way -- all I found related to column is that I can specify header template. How to build a cell template for it and set the widget to textbox?
int i = 0;
foreach (var db_col in query.FieldNames)
{
var col = new DataGridTextColumn();
col.IsReadOnly = false;
col.Header = db_col;
col.Binding = new Binding(String.Format("Visual[{0}]", i));
grid.Columns.Add(col);
++i;
}
Thank you in advance.
Solved
Thanks to Marko, I finally came up with such solution.
foreach (var db_col in query.FieldNames)
{
var template = new DataTemplate();
var elemFactory = new FrameworkElementFactory(typeof(TextBox));
elemFactory.SetBinding(TextBox.TextProperty, new Binding(String.Format("Visual[{0}]", i)));
template.VisualTree = elemFactory;
var col = new DataGridTemplateColumn();
col.CellTemplate = template;
col.IsReadOnly = true;
col.Header = db_col;
grid.Columns.Add(col);
++i;
}