How to stop WPF Grid from automatically setting fields on children

I have the most annoying issue with Visual Studio 2012 and WPF, which I was hoping to get some help for. I have a simple grid with children, say TextBlocks, with grid cells. TextBlocks are configured to place RELATIVELY inside a cell (for example, using HorizontalAlignment of Left). Everything is fine until I dare to resize the column or row of the grid. Visual Studio then inserts the Margin and Width / Height attributes to match the resulting positions of the parent / child elements. All this happens quietly, and I do not notice it until I decide to reduce the size of the column further, and suddenly the TextBlock is clipped due to the Margin attributes that were inserted. I need to go through each child element and manually remove these additional attributes. I had to do this close to 100 times in my current project, and I cannot find anything on the Internet to figure out how to tell Visual Studio BACK OFF and stop accepting these freedoms that will steal the good formatting that I'm trying to achieve. Here The code snippet is for reference. But this happened in many different cases of grids in my project.

<Grid Canvas.Left="435" Canvas.Top="138" Height="61" Width="192"> <Grid.ColumnDefinitions> <ColumnDefinition Width="11*"/> <ColumnDefinition Width="21*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Text="Subtotal:" HorizontalAlignment="Right" VerticalAlignment="Center" /> </Grid> 

... and after resizing column 0:

 <TextBlock Grid.Row="0" Grid.Column="0" Text="Subtotal:" HorizontalAlignment="Right" VerticalAlignment="Center" Height="16" Margin="0,7" Width="47" /> 

Of course, there are others who have encountered this problem. Please, help!

thanks

+4
source share
4 answers

In XAML's design, little can be done with the odd automatic behavior. As a generic word for the wise, you are often better off using a XAML text editor instead of a designer to edit your xaml.

Fortunately, there is a solution in your special case. The behavior depends on where you acoustically capture the column with the mouse:

If you take it at the top, it will set new fields. grab at the top

if you take it below (on the line), it will not set / change fields. grab at the bottom

Update: this feature no longer exists in VS 2012.

Now you can change the column resizing behavior using the Ctrl and / or Shift keys. But none of the actions gives the result that you are after.

If all your elements in the grid do not have a set of Width and Margin , it is best to resize the columns without a modifier key (in VS2012 it should not destroy the layout visually, it should just add the unnecessary Margin , Height and Width properties), and then in the " The structure of the document "select all the elements and in the" Properties "view, click the" Configure on auto "button. This will remove unnecessary properties.

Set to auto

+4
source

A simple solution is to simply not use the design surface for anything other than a visual reference.

I use a text editor, because otherwise you have practically no control over your XAML - I find it very annoying that dragging a control on the design surface automatically sets the margins and alignment, etc.

The way I see it - everything in XAML should automatically scale - the only time you need to set the absolute value to something, when you absolutely need it to be that size ... and even then it is probably better to do (for example, the standard button should not be scaled to the width of the entire application - therefore, the explicit size is fine ...), the fields should always be used to set fields on elements, and not used for layout (which, apparently, seems to be a designer looks normal)

I use the design surface as a link β€” you can also click an element and the XAML code will synchronize with the element you clicked on (most of the time) β€”but for love of God, don’t drag anything or you will end up in a world of pain!

+1
source

I set the fields for all elements on all sides, and if they get reset, I set them again. Without setting fields, the XAML designer also inserts both fields and spaces that break everything.

0
source

I know this question is very old. But I found a simple solution for any version of Visual Studio. I think this can be very useful for those who are facing the same problem.

First drag the top as shown in the image below:

enter image description here

Leave your mouse and note the column width for each column.

Press Ctrl + Z to cancel.

Then change the column width from XAML.

Now you will get the required column size, and no additional margins or height or width will be added to any of your components inside the grid.

0
source

Source: https://habr.com/ru/post/1438933/


All Articles