xaml and C# should be equivalent, but they're not

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);

Upvotes: 1

Views: 305

Answers (1)

Joel Lucsy
Joel Lucsy

Reputation: 8706

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.

Upvotes: 2

Related Questions