c# - WPF grid layout and cell content margins -


i working on wpf control content grid. relatively new wpf wondering if below right way go this.

i placed 2 labels in grid, both in same column adjacent rows:

<window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:class="untitledproject8.window1" x:name="window" title="window1" width="200" height="200">  <grid x:name="layoutroot">     <grid horizontalalignment="left" verticalalignment="top" width="100" height="100"/>     <grid.rowdefinitions>         <rowdefinition height="*"/>         <rowdefinition height="*"/>     </grid.rowdefinitions>     <label grid.row="0" content="1.23" fontsize="18" horizontalalignment="center" verticalalignment="bottom"/>     <label grid.row="1" content="45" fontsize="48" horizontalalignment="center" verticalalignment="top"/> </grid> 

i set labels' vertical alignment label on row 0 aligned bottom , label on row 1 aligned top.

now, close want need actual text of label in row 1 closer text of label in row zero. set margin of label in row 1 negative value:

<window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:class="untitledproject8.window1" x:name="window" title="window1" width="200" height="200">  <grid x:name="layoutroot">     <grid horizontalalignment="left" verticalalignment="top" width="100" height="100"/>     <grid.rowdefinitions>         <rowdefinition height="*"/>         <rowdefinition height="*"/>     </grid.rowdefinitions>     <label grid.row="0" content="1.23" fontsize="18" horizontalalignment="center" verticalalignment="bottom"/>     <label grid.row="1" content="45" fontsize="48" horizontalalignment="center" verticalalignment="top" margin="0,-20,0,0"/> </grid> 

is right way it? granted examples above simplistic grid contents grow , more complicated (such including other layout containers) setting different values control margins correct way make adjacent cells closer or farther apart?

i little concerned since trying best avoid hard coding types of "designer" values did when working winforms (such setting exact coords location , values sizes) , let layout manager take care of it. however, looks setting margin way go.

thanks :)

that looks good! thing caught little off guard -20 top margin (instead of 20 bottom should same thing), change clarity.

the main thing note container of choice, grid work you. feature of stretch grid, distance between elements proportionally grow (probably want anyway). weakness of grid it's not efficient. because can it.

you accomplish same thing above canvas (with stretching feature) or if didn't want distance stretch might try stackpanel, going more efficient grid. there few other panels well, becoming acquainted can (and how perform) helpful, when having create more complex layouts.

as margin, yeah that, along setting width , height, standard ways of setting spacing.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -