Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop WPF Grid from automatically inserting margins on child elements

I have the most annoying problem with Visual Studio 2012 and WPF I was hoping to get some help with. I have a simple Grid with child elements, say, TextBlocks, with the grid's cells. The TextBlocks are setup to be positioned RELATIVELY within the cell (using HorizontalAlignment of Left, for example). Everything is fine until I dare to resize its column or row of the Grid. Visual Studio then inserts Margin and Width/Height attributes in keeping with the resulting positions of parent/child elemnts. This all happens silently and I don't notice it until later if I decide to reduce the size of the column further, all of the sudden the TextBlock gets clipped, due to its Margin attributes that were inserted. I have to go through every child element and remove these extra attributes manually. I have had to do this close to 100 times on my current project, and I cannot find anything on the web to figure out how to tell Visual Studio to BACK OFF and stop taking these liberties that steal the nice formatting I'm trying to achieve. Here's a code snippet for reference. But this has happened on many different instances 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 the resizing of column 0:

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

Surely there are others out there who have experienced this issue. Please help!

Thanks

like image 694
BCA Avatar asked Oct 10 '12 14:10

BCA


1 Answers

There is not much you can do about the odd automatic behaviors in the XAML designer. As a general word to the wise, you are often better of using XAML text editor instead of the designer to edit your xaml.

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

If you grab it at the top it will set new margins. grab at the top

if you grab it below (at the line) it will not set/change margins. grab at the bottom

Update: This feature does not exist in VS 2012 anymore.

You can now change the behavior of a column resize with the Ctrl and/or the Shift key. But none of the behaviors yield the result that you're after.

In case all your items within the grid do not have a Width and a Margin set, your best bet is to resize the columns without any modifier key (in VS2012 it should not destroy the layout visually, it should just add the unecessary Margin, Height and Width properties) and then in the Document Outline window multiselect all the elements and in the Properties view Klick the Set To Auto button. This will delete the unnecessary properties.

Set To Auto

like image 64
bitbonk Avatar answered Sep 25 '22 19:09

bitbonk