TheJediCowboy
TheJediCowboy

Reputation: 9232

Width and MaxWidth properties not working for WPF ColumnDefinitions

I am working with on an application that I am Dragging and Dropping Items, and creasting grids dynamically based on the contents of the objects I am dropping(some objects will require me to create a grid with 2 columns, some with 4,etc). This is simple enough to do, but when I try to specify how wide I would like these columns to be, it is not working, and it is showing the grids on top of eachother. I will attach what the columns look like.

The first example shows the result of 5 objects being dragged and dropped. You can see that the the width specification has no effects. enter image description here

The second example is the same thing, but shows that the grids are not following the width specifications. When I go to drop an "Email" object onto the "Zip" object, it overlays the grids. enter image description here

Here is the code that I am using to create the definitions. As you can see for each Item I iterate over, I am creating a label for.

                /*Initialize Grid Layout*/
                Grid newGrid = new Grid();
                newGrid.MinHeight = 40;

                /*Define Column Definitions*/
                List<ColumnDefinition> columns = new List<ColumnDefinition>  (fieldItemList.Count);
                foreach (ColumnDefinition column in columns)
                {
                    ColumnDefinition labelColumn = new ColumnDefinition();

                    /*Specify Width Dimensions*/
                    labelColumn.Width = new GridLength(150);
                    labelColumn.MaxWidth = 200.0;
                    newGrid.ColumnDefinitions.Add(labelColumn);
                    newGrid.ColumnDefinitions.Add(column);
                }

                /*Define Row Definitions*/
                RowDefinition row = new RowDefinition();
                newGrid.RowDefinitions.Add(row);

How do I get it to respect the width boundaries I am assigning to the column definitions?

Upvotes: 0

Views: 1548

Answers (3)

grantnz
grantnz

Reputation: 7423

Is that your actual code? If so, I don't think you are adding any columns to the grid.

  List<ColumnDefinition> columns = new List<ColumnDefinition>  (fieldItemList.Count);
  foreach (ColumnDefinition column in columns)
            {

In the above code, the columns object is being initialized with a capacity of fieldItemList.Count but doesn't contain any ColumnDefinition objects.

Upvotes: 1

sourcenouveau
sourcenouveau

Reputation: 30554

You are adding (2 * fieldItemList.Count) columns. I don't think you should be adding the columns in the loop... try defining the Grid first in XAML and then porting it to C#.

Upvotes: 0

John Bowen
John Bowen

Reputation: 24453

Try setting newGrid.HorizontalAlignment = HorizontalAlignment.Left;

If the Grid is set to stretch and has only fixed width columns it doesn't have anything to fill the remaining space it needs to fill. You could also add an extra * sized column to take up any remaining space.

Upvotes: 0

Related Questions