Disable Auto Grid Resizing in XAML Designer using VS 2019 - wpf

This very frustrating problem keeps happening (now in vs 2019 and before in vs 2017) and after finding zilch looking through the XAML options and searching for similar issues I was hoping someone can help with this:
Selecting elements in the designer (particularly grid elements) will sometimes alter the * grid definitions to crazy values that will LOOK the same visually but then when I look at them markup at some point the ratios have gotten HUGE or even it will add extra rows/cols and then automatically change the spans of what is actually there its nerveracking!
Any ideas what this awful setting is or what I'm clicking to make this happen?
For example it will change this:
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="7*" />
To this:
<ColumnDefinition Width="37*" />
<ColumnDefinition Width="30" />
<ColumnDefinition Width="513*" />
And it never affects the view in the designer just the markup. That's as much explanation I can give offhand, any help would be great!

The ratio of rows to columns in a Grid is very precise. For your first ColumnDefinition, means dividing the width of the whole Grid into ten parts,the first column is 20% of the width of Grid, second is 10%,third is 30%.This is desgined by your need.
When you do a little modifity in the designer, all column widths maybe distributed proportionally if you choose Star for the ColumnDefinition in the designer as the below picture shown:

Related

How do I select a row in a WPF Grid control?

I'm using a Grid to display data which is not known until run-time. The XAML for my Grid is very simple since I add controls to it programmatically. I need the grid to be flexible.
I would like the user to be able to select(highlight) the entire row in the grid and then be able to click on a button to process the data in that row. How could I do this?
I have not been able to find any information related to my problem. Any ideas would be greatly appreciated.
Here is the XAML:
<Grid x:Name="lstAssigned" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
</Grid>
Thanks Everyone!
Here are some more details:
The data to be displayed will vary. The grid will have 5 columns. Columns 2 and 3 will be combo boxes. The other columns are textBoxes. The user will enter data and save it.
Another time, the data to be displayed could be: combo boxes in columns 2 and 4, and a date in column 5. Since my data source will vary, I was trying to set the control type in each column programmatically.
I initially started with a DataGrid using DataTemplates, but this would define the columns and order. Am I mistaken? I want to define them at run-time.
What would be the best way to handle this? What type of control should I use?
I would apprectiate any kind of adice you can offer.
Thanks in advance.
This sounds like you should use 2 separate DataGrids for each case. There are ways to alter the columns programatically in runtime, but it's more messy and leads to less maintainability. If I were given this task, I would simply use 2 Different DataGrids,
one for case #1, where you need
Text Combo Combo Text Text
and the other for case #2:
Text Combo Text Combo DateTime
Sounds like a really simple set, where there are no major headaches, then you could just create a proper DataTemplate containing each of this DataGrids for each type of Model object.
Grid doesn't support selection of rows/columns/cells. It's used to layout controls for display. Use a something else, like a ListView instead.

Grid inside a StackPanel: why do auto and * behave strangely?

My google and stackoverflow search-fu have failed me, so I present to the community this question.
(This is all generated using VS2010 and .NET 4.0, in a blank default WPF Solution)
Consider the following XAML:
<StackPanel Orientation="Horizontal">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Name="aborder" Grid.Column="0" Grid.ColumnSpan="2"
Background="Red" Width="200"/>
<Border Name="aborder2" Background="Green"/>
</Grid>
</StackPanel>
What would you predict the width of "aborder2" to be?
If you guessed "20 pixels", you would be wrong. The correct answer is 110 pixels.
Consider this XAML:
<StackPanel Orientation="Horizontal">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Border Name="aborder" Grid.Column="0" Grid.ColumnSpan="2"
Background="Red" Width="200"/>
<Border Name="aborder2" Background="Green"/>
</Grid>
</StackPanel>
What would you predict the width of "aborder2" to be?
If you guessed either 20 pixels or 110 pixels, you would be wrong. The correct answer is 200 pixels.
I cannot figure this out and it's driving me insane. It seems like the answer should be obvious; clearly there's some interaction between an auto-filling grid column and the stackpanel that causes the grid to freak out. But it just doesn't seem to make sense - whatever rules are governing this behavior seem to be arbitrary. Why 110 pixels? Why not 109 pixels or 100 pixels? I would understand if the auto-sized column failed to expand fully or something, but to have the fixed-width column randomly ignore its width has left me a burnt out shell of a developer.
Any help or guiding lights would be much appreciated!
I have no idea why the first example isn't rendering correctly
The 2nd is because Auto means "the same size as the contents", but you have nothing in Column2 so Column2 is getting rendered at 0px. You have something in Column1 which spans 2 cells, but since Column2 is rendered at 0 px it means Column1 is stretched to 200 px. By default, Grid's expand their children to fill all available space in the Cell, so this is making aborder2 stretch to 200px instead of 20.
I think the first example might be a similar situation, where Column2 is rendering at 0px because it has no content, however I am not sure why it is setting aborder2 to a width of 110. The 110 seems to come from (GridWidth / TotalColumns) + (1stColumnWidth / TotalColumns * NumberOfStarColumns), so I think it's a bug.
As a side note, you can set a Column1's MaxWidth="20" to force Column1 to always render as 20px
No answer for you, but it seems to happen in Silverlight too. I'd assume there's some bug in the arrange and measure passes. If you put a custom control in there instead of a border and override the measure and arrange methods you would probably get a better picture of what's going on.
To try to solve the mystery of where 110 comes from, I'm guessing (200 - 20) / 2 + 20
EDIT: I tried a few other formulas and it didn't hold up, looks more like:
(200 + 20) / 2

How to restore controls in their correct places same as earlier when we maximize a window?

I am creating a wpf application.It contains a ListBox in which there are 3 buttons that are to be put on extreme right hand side one after another .The problem is that when I maximize that window ,the buttons does not take their correct places as used to be in earlier window i.e. They should be on the extreme right hand side of the form one after another.But now they are shown in the middle of the ListBox..Please guys help me to solve this problem ASAP..
With no code to go on, I can only assume you're using absolute positioning. That would explain why it only works when your Window is a certain size. If you instead use relative positioning, you can achieve your goals.
There are many ways to do this, and you should read up on WPF layout if you're unfamiliar with it. Here is one example:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ListBox/>
<StackPanel Grid.Column="1">
<Button>One</Button>
<Button>Two</Button>
<Button>Three</Button>
</StackPanel>
</Grid>

WPF DataGrid Column Width

I have a DataGrid in WPF with 3 columns. I would like these columns to take up all the space available in the grid. So for example:
Column 1 takes 40% of the grid's width
Column 2 takes 30% of the grid's width
Column 3 takes 30% of the grid's width
Such that even when resizing the window or grid the columns width resizes accordingly. Anyway I can achieve this.
Thanks
Regards
Gabriel.
I see you already found the answer you were looking for based on your comment. However, in case anybody else runs across this question trying to figure out how to get the column ratios (like your example of Column 1 = 40%, Column 2 = 30%, Column 3 = 30%), you can specify the ratios with * sizing for column widths as follows:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
</Grid>

Looking for explanation for WPF Grid ColumnSpan behavior

I asked a question at http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c7f5cdf-4351-4969-990f-29ce9ec84b87/ , but still lack a good explanation for a strange behavior.
Running the following XAML shows that the TextBlock in column 0 is width greater than 100 even though the column is set to width 100. I think that the strangeness may have something to do with it being wrapped in a ScrollViewer, but I don't know why. If I set a MaxWidth on the columns, it works fine, but setting Width does not.
Why is the width of column 0 not being honored?
Why does the column sizing behave differently when you remove the scroll viewer?
I appreciate any explanation! This is a real puzzle to me.
<Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Width="300">
<ScrollViewer HorizontalScrollBarVisibility="Auto" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock x:Name="textBlock" Text="{Binding ElementName=textBlock, Path=ActualWidth}" />
<TextBlock Text="column 1" Grid.Column="1" />
<TextBlock Grid.Row="1" Grid.ColumnSpan="3" Text="text here that is wider than the first two columns combined" />
</Grid>
</ScrollViewer>
</Window>
This is a very good question and tests the limits of our intuition. It reveals the implementation details of the Grid's layout logic.
The width of 100 is not being honored because:
There is nothing in the third column that causes the grid to give it width.
The long text in the second row is wider than can fit in the first two columns.
When the width of the Grid is not constrained or set by its parent its layout logic evidently stretches the first column instead of the last column.
By putting a MaxWidth on the first column, you are constraining the Grid's layout logic, so it moves on to the second column and stretches it. You'll note it will be wider than 100 in that scenario.
However, as soon as the Grid's width is set to a specific value or is constrained by its parent (e.g. when no ScrollViewer in the Window), the Grid's width has a specific value, and the third column gets a width set even though it is empty. Now the Grid's auto-size code is deactivated, and it no longer stretches any of your columns to try to squeeze in that text. You can see this by putting a specific width on the Grid, even though it is still in the ScrollViewer.
Edit: Now that I read the answer of the MSDN support in your original thread, I believe it is correct, meaning this is probably the result of the implementation of the attached property and not the grid itself. However, the principle is the same, and hopefully my explanation is clear enough to make sense of the subtlety here.
Short Answer:
Its because of the combination of:
1. Presence of ScrollViewer which allows grid (if it wishes) to take any desired size.
2. The grid not having explicit width.
3. A column (Column 2) whose width has not been specified, which sets it to 1*, leaving its final size dependant on size of grid and other columns.
4. TextBlock which has colspan over three columns.
If you:
1. Remove the scrollviewer, the grid is allowed to grow only till the client area of window (which comes to be about 278 in your example), and the long textblock has to fit within this width otherwise its trimmed.
2. Set explicit width of Grid, which again trims textblock to fit.
3. Set explicit width of Column 2, which provides a fixed width to grid (100+100+width_of_col2), which again trims textblock to fit.
4. Remove the colspan, the columns which do not contain it and have fixed width defined, will take that width.
Here's what's happening:
This is crude and not exact explanation of the measure and arrange passes, however, it should give a fair idea.
To start with col0 is happy with 100, col1 with 100 and col2 with 0. Based on this grid's size would be 100+100+0=200. When Grid requests its children (textblocks) to be measured, it sees that first two textblocks fit within the width of their columns. However, the third textblock needs 288. Since, grid isn't having any width defined and its within a scrollviewer, it can increase its size if one of its child needs it. The Grid has now to increase its size from 200 to 288 (i.e. by 88). This means each column to which that textblock spans (all three of them) will expand by 88/3~=29 pixels. This makes col0=100+29=129, col1=100+29=129, col2=0+29.
Try this:
Include a rectangle, put it in col2 and set width of rectangle to 20.
This is what's happening:
To start with col0 and col1 are happy with 100 each as their individual textblocks need less than that. col2 is happy with 20 as rectangle in it needs that. Based on this grid's width would be 100+100+20=220. However, because of the columnspanning textblock the Grid has to increase its size from 220 to 288 (i.e. by 68). This means each column to which that textblock spans (all three of them) will expand by 68/3~=23 pixels. This makes col0=100+23=123, col1=100+23=123, col2=20+23=43.
HTH.
Here is another example that shows the problem using a Canvas instead of a ScrollViewer:
<Canvas>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" x:Name="textBlock1" Text="{Binding ElementName=textBlock1, Path=ActualWidth}"/>
<TextBlock Grid.Column="1" x:Name="textBlock2" Text="{Binding ElementName=textBlock2, Path=ActualWidth}"/>
<TextBlock Grid.Row="1" Grid.ColumnSpan="3" Width="300"/>
</Grid>
</Canvas>
This example shows that when given unlimited space, the first two columns are incorrectly expanded by 33%. I do not have working reference source to debug this right now because SP1 broke .NET4 reference source but frankly pinpointing this to the line in the source file is not going to help you so let's not go that route.
Instead, we'll agree that this is definitely a bug and we can prove that it's a bug by setting Grid.MaxWidth to progressively larger values and the widths of two columns remain both at 100 no matter how large it gets. But if you leave Grid.MaxWidth unset and place the Grid inside of a Canvas then the value during measure will be double.PositiveInfinity and this value with produce column widths of 133. As a result we can speculate that some how the special case of a size constraint of positive infinity is not handled correctly during the column sizing calculations.
Luckily, that same experiment provides a simple workaround: simply supply an absurdly large value for Grid.MaxWidth when the Grid is used inside another control that allows it unlimited space, such as a ScrollViewer or a Canvas. I recommend something like:
<Grid MaxWidth="1000000">
This approach avoids the bug by preventing the size constraint from having the probematic value of positive infinity, while practically achieving the same effect.
But this:
<Grid MaxWidth="{x:Static sys:Double.PositiveInfinity}">
will trigger the bug.
I reported this issue as a bug:
https://connect.microsoft.com/VisualStudio/feedback/details/665448/wpf-grids-columns-width-property-not-honored-when-columnspan-row-forces-grid-to-grow
Please vote it up at that link if you agree that it is a bug.

Resources