I'm working in VB.Net w/ WPF and I need to create a DataGrid at run-time (in the code-behind) with the text in each grid cell able to wrap. I've found a lot of examples showing how to do this in the XML but I can't find anything explaining how this is done in the code-behind. I'm stuck trying to figure out how the DataGridTemplateColumn, CellTemplate, and DataTemplate all work together. Any help on this would be greatly appreciated.
I think this is equivalent to what I'm trying to do:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock TextWrapping="Wrap" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Update:
I've made some progress but the cell text wrapping is still not working... Here is what I've got so far:
Dim grid As New DataGrid
grid.AutoGenerateColumns = False
Dim dgtc As New DataGridTemplateColumn
dgtc.Header = "Test"
dgtc.Width = 200
Dim factory1 As New FrameworkElementFactory(GetType(TextBlock))
Dim b1 As New Binding("WrapDirection.Right")
factory1.SetValue(TextBlock.TextWrappingProperty, b1)
Dim dt As New DataTemplate
dt.VisualTree = factory1
dgtc.CellTemplate = dt
grid.Columns.Add(dgtc)
I think the problem is with the binding and the textblock property. Something like this would make more sense but it doesn't work...
Dim b1 As New Binding("Wrap")
factory1.SetValue(TextBlock.TextWrapping, b1)