1

I have created a one-row grid in xaml, and then add rows dynamically in C#. Yet, although they ALMOST line up, they don't QUITE do so. Why would this be, when the code is the same?

The most pertinent part of the XAML is:

<Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="4*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="3*" /> <ColumnDefinition Width="2*" /> <ColumnDefinition Width="2*" /> </Grid.ColumnDefinitions> 

The most pertinent part of the C# is:

Grid grd = new Grid(); ColumnDefinition c0 = new ColumnDefinition(); c0.Width = new GridLength(1, GridUnitType.Star); ColumnDefinition c1 = new ColumnDefinition(); c1.Width = new GridLength(1, GridUnitType.Star); ColumnDefinition c2 = new ColumnDefinition(); c2.Width = new GridLength(1, GridUnitType.Star); ColumnDefinition c3 = new ColumnDefinition(); c3.Width = new GridLength(4, GridUnitType.Star); ColumnDefinition c4 = new ColumnDefinition(); c4.Width = new GridLength(1, GridUnitType.Star); ColumnDefinition c5 = new ColumnDefinition(); c5.Width = new GridLength(3, GridUnitType.Star); ColumnDefinition c6 = new ColumnDefinition(); c6.Width = new GridLength(2, GridUnitType.Star); ColumnDefinition c7 = new ColumnDefinition(); c7.Width = new GridLength(2, GridUnitType.Star); grd.ColumnDefinitions.Add(c0); grd.ColumnDefinitions.Add(c1); grd.ColumnDefinitions.Add(c2); grd.ColumnDefinitions.Add(c3); grd.ColumnDefinitions.Add(c4); grd.ColumnDefinitions.Add(c5); grd.ColumnDefinitions.Add(c6); grd.ColumnDefinitions.Add(c7); // Add it to the StackPanel spNufan.Children.Add(grd); 

Screenshot http://getfile6.posterous.com/getfile/files.posterous.com/temp-2012-02-16/EHnCudinikxcfprHxjywowEcwBssAcpbdchkeinAqJdhvuwDIhJxoonrjGEq/MisalignedColumns.bmp.scaled1000.jpg

Updated

It seems I have to choose between having all my columns line up and having a really wide form OR allowing "cells" to wrap with wiggly/wavy column alignment.

At first I had wrapping working fine, but the columns weren't exactly aligned, but now that I've added the following (SharedSizeGroup element) to my xaml:

<StackPanel x:Name="spNufan" Grid.IsSharedSizeScope="True" Orientation="Vertical"> <Grid ShowGridLines="True" > <Grid.ColumnDefinitions> <ColumnDefinition SharedSizeGroup="ZeroethColumn" Width="*" /> <ColumnDefinition SharedSizeGroup="FirstColumn" Width="*" /> <ColumnDefinition SharedSizeGroup="SecondColumn" Width="*" /> <ColumnDefinition SharedSizeGroup="ThirdColumn" Width="4*" /> <ColumnDefinition SharedSizeGroup="FourthColumn" Width="*" /> <ColumnDefinition SharedSizeGroup="FifthColumn" Width="3*" /> <ColumnDefinition SharedSizeGroup="SixthColumn" Width="2*" /> <ColumnDefinition SharedSizeGroup="SeventhColumn" Width="2*" /> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Margin="2" TextAlignment="Left" TextWrapping="Wrap" . . . 

...it no longer does (allow the cells to wrap as they should), although I have TextWrapping set to "Wrap"

Here is my pertinent C#:

Grid grd = new Grid(); grd.ShowGridLines = true; ColumnDefinition c0 = new ColumnDefinition(); c0.Width = new GridLength(1, GridUnitType.Star); c0.SharedSizeGroup = "ZeroethColumn"; . . . spNufan.Children.Add(grd); . . . TextBlock tbDateTime = new TextBlock(); tbDateTime.Margin = _margin; tbDateTime.Background = scb; tbDateTime.TextWrapping = TextWrapping.Wrap; tbDateTime.TextAlignment = TextAlignment.Left; tbDateTime.VerticalAlignment = VerticalAlignment.Center; tbDateTime.Text = ADateTime.ToString(); Grid.SetColumn(tbDateTime, LogParsePumpViewerConsts.GRID_COL_DATETIME); grd.Children.Add(tbDateTime); 
6
  • 3
    Can you provide screenshots to show visually how they differ? Commented Feb 16, 2012 at 18:07
  • Do you have auto generate columns set to true..? if so set that to false also this spNufan.Children.Add(grd); looks like something from a post on here yesterday.. for the .Children.Add() method Commented Feb 16, 2012 at 18:11
  • And did you carefully check all Margin and Padding settings? Commented Feb 16, 2012 at 18:27
  • Is there a way to add scream shots here? Commented Feb 16, 2012 at 19:01
  • I temporarily added a screen shot here: warbler.posterous.com/misaligned-columns Commented Feb 16, 2012 at 19:04

1 Answer 1

2

You have different content in the orange group as compared to the red group. The text overflow will make the column sizes slightly different. Most notably in the first column, 2/12/2012 is larger than 2/8/2012.

Sign up to request clarification or add additional context in comments.

4 Comments

Ah, yes, each row's columns are relative to its own row, so that makes sense. So...is there a way to force each row to use the same widths as the first one without specifying absolute widths?
Yes, columns can share a size. Peruse wpf.2000things.com/2011/11/07/… for details.
I tried setting SharedSizeGroup via <ColumnDefinition SharedSizeGroup="ZeroethColumn" Width="*" /> in xaml and c0.SharedSizeGroup = "ZeroethColumn"; in C#, but it's still skewed - some of the columns bear a striking resemblance to the Leaning Tower of Pisa.
Adding Grid.IsSharedSizeScope="True" to the StackPanel's properties in xaml does make all the column widths uniform across rows, but it disregards the "TextWrapping = Wrap" of the individual columns, thus ending up with a form that is so wide not even Evel Knievel would have dared attempt to jump it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.