Reputation: 49
There must be a simple solution for this, but I just can't find it. I have a DataGrid with DataGridTextColumns which contain data. This is working fine. I just seem to not get the String inside the Cells to make a line break, instead everything is in one line (no matter how long the string is).
dataGrid.IsReadOnly = true;
dataGrid.AutoGenerateColumns = false;
dataGrid.ItemsSource = ConvertListIntoPlantViewModel(lList);
DataGridTextColumn textColumn1 = new DataGridTextColumn();
DataGridTextColumn textColumn2 = new DataGridTextColumn();
DataGridTextColumn textColumn3 = new DataGridTextColumn();
textColumn3.MaxWidth = 200;
textColumn1.Header = "Name";
textColumn1.Binding = new Binding("Name");
textColumn2.Header = "Type";
textColumn2.Binding = new Binding("Type");
textColumn3.Header = "Info";
textColumn3.Binding = new Binding("Information");
dataGrid.Columns.Add(textColumn1);
dataGrid.Columns.Add(textColumn2);
dataGrid.Columns.Add(textColumn3);
Let's say I want to make the Text, which ends up in textColumn3 to make a line break at the end of the cell (which is 200 wide). How do I achieve this? There must be a simple Style Element that can be set, right? I just can't find it.
Help would be appreciated, thanks!
Upvotes: 2
Views: 4337
Reputation: 169150
There must be a simple Style Element that can be set, right? I just can't find it.
Yes, you could set the ElementStyle property of the column(s) to the following Style for the text to wrap:
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
You could create the Style programmatically using the XamlReader.Parse method:
string xaml = "<Style xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" TargetType=\"TextBlock\"><Setter Property=\"TextWrapping\" Value=\"Wrap\"/></Style>";
Style style = System.Windows.Markup.XamlReader.Parse(xaml) as Style;
DataGridTextColumn textColumn1 = new DataGridTextColumn();
textColumn1.ElementStyle = style;
Upvotes: 4