Using C# and WPF, I am trying to wrap the text in a DataGrid Column. I am setting the ItemsSource of the DataGrid in the code-behind, rather than using data binding.
As far as I can tell, there isn't a 'wrap' property for a datagrid, but it is possible to nest cell data inside of a textblock that can be wrapped. Unfortunately everything I've seen to do this uses data binding in the xaml, which would require a lot of refactoring, so I am trying to avoid it.
My xaml item:
<DataGrid x:Name="dgvOrderItems" AutoGeneratedColumns="DgvOrderItems_AutoGeneratedColumns" Height="570" VerticalAlignment="Stretch" Width="auto" HorizontalAlignment="Stretch" FontSize="10" Padding="0" Margin="0,20,0,0" ></DataGrid>
My code behind for the DgvOrderItems_AutoGeneratedColumns:
dgvOrderItems.SelectionUnit = DataGridSelectionUnit.FullRow;
dgvOrderItems.IsReadOnly = true;
dgvOrderItems.ColumnHeaderHeight = 15;
dgvOrderItems.Columns[0].Header = "Order Item";
dgvOrderItems.Columns[0].Width = 113;
dgvOrderItems.Columns[1].Visibility = Visibility.Hidden;
dgvOrderItems.Columns[2].Header = "Qty.";
dgvOrderItems.Columns[2].Width = 25;
As yet, I have been unable to find a good way to wrap the "Order Item" if the length exceeds the specified width.
site:StackOverflow.com create datatemplate programmatically. It's a finicky tedious hassle. You could create it properly in XAML as a resource, and use FindResource to load it. If you're not going to refactor, that's your best bet by miles. At least start getting your feet wet here and there with XAML before committing to a full rewrite. How big is this project? - 15ee8f99-57ff-4f92-890c-b56153