-1

I am trying to fill a Grid in a WPF application dynamically.

I'm creating a row and then some columns. in each column I am adding a textblock, like this:

GrdMainGrid.RowDefinitions.Add(new RowDefinition(Height = new GridLength(150, GridUnitType.Pixel);
GrdMainGrid.RowDefinitions.Add(new RowDefinition(Height = new GridLength(1, GridUnitType.Star);

for(var i=0; i<4; i++)
{
  GrdMainGrid.ColumnDefinitions.Add(new ColumnDefinition {With = new GridLength(25, GridUnitType.Pixel)});
}
var header = new TextBlock {Text = "Header1", RenderTransform = new RotateTransform(-90), Width = 150, Margin = new Thickness(0), VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Left};
.
.
.
Grid.SetColumn(header, 0);
Grid.SetRow(header, 0);
.
.
.
GrdMainGrid.Children.Add(header);

The textblock is rotated -90 degree. While the size of the column is set to 25 pixel i doesn't show all text, when I increase the size of the column the text in textblock is also increased. see pic enter image description here

enter image description here

I could understand this if the textblock wasn't rotated and it didn't fit in the column. But what has it to do with the size of the column when it's rotated. And is it possible somehow to decrease the width of the column without decreasing size of text ? Tnx in advance.

Nawed Nabi Zada
  • 2,819
  • 5
  • 29
  • 40

1 Answers1

4

Use a LayoutTransform instead of a RenderTransform.

    var header = new TextBlock { Text = "Header1", LayoutTransform = new RotateTransform(-90), Width = 150, Margin = new Thickness(0), VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Left };
mdm20
  • 4,475
  • 2
  • 22
  • 24