Form derailed after adding a Scrollviewer

Asked

Viewed 49 times

0

I had a form ready in my VS, After it was ready, I realized that I would need to add a Scrollbar. Searching and saw that to solve my problem was just add a Scrollviewer. When I add, my form gets all out of order, a label on one side and a textbox on the other. Because???

Help me =(

<Controls:MetroWindow x:Class="WpfSistemaDinamCorp.Telas.CadastroEmpresa"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    Title="Cadastro de Empresa" Height="1081" Width="1400" WindowState="Maximized" UseLayoutRounding="False" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto">
<Grid Margin="0,0,18,21" UseLayoutRounding="False" ScrollViewer.CanContentScroll="True">
    <Grid.RowDefinitions>
        <RowDefinition Height="4*"/>
        <RowDefinition Height="45*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <ScrollViewer VerticalScrollBarVisibility="Auto" Margin="0,0,0,52" Grid.RowSpan="2">
        <StackPanel>
    <Label Content="CADASTRO DE EMPRESA" VerticalAlignment="Top" Height="43" Width="302" FontSize="24" HorizontalAlignment="Center" Margin="528,0,544,0"/>
    <Label Content="Código:" HorizontalAlignment="Left" Margin="77,2,0,0" VerticalAlignment="Top" Grid.Row="1"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="133,3,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="190" Grid.Row="1"/>
    <Label Content="CNPJ / CPF:" HorizontalAlignment="Left" Margin="60,33,0,0" VerticalAlignment="Top" Grid.Row="1"/>
    <Label Content="Razão Social / Nome:" HorizontalAlignment="Left" Margin="9,64,0,0" VerticalAlignment="Top" Grid.Row="1"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="133,34,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="190" Grid.Row="1"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="133,65,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="559" Grid.Row="1"/>
    <Label Content="Nome Fantasia:" HorizontalAlignment="Left" Margin="39,121,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="133,121,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="290"/>
    <Label Content="CEP:" HorizontalAlignment="Left" Margin="98,152,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="133,152,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="158"/>
    <Label Content="Código IBGE:" HorizontalAlignment="Left" Margin="52,184,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="133,183,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="158"/>
    <Label Content="Tipo de Logradouro:" HorizontalAlignment="Left" Margin="13,215,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="133,215,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="158"/>
    <Label Content="Inscrição Estadual:" HorizontalAlignment="Left" Margin="25,246,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <Label Content="Inscrição Municipal:" HorizontalAlignment="Left" Margin="17,278,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <Label Content="RNTRC:" HorizontalAlignment="Left" Margin="81,309,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="133,246,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="190"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="133,278,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="190"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="133,309,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="190"/>
    <Label Content="Apelido:" HorizontalAlignment="Left" Margin="474,121,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="528,121,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="240"/>
    <Label Content="Site:" HorizontalAlignment="Left" Margin="819,121,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="851,121,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="244"/>
    <Label Content="Município:" HorizontalAlignment="Left" Margin="355,184,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="425,183,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>
    <Label Content="Bairro:" HorizontalAlignment="Left" Margin="693,184,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="742,183,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="180"/>
    <Label Content="Estado (UF):" HorizontalAlignment="Left" Margin="967,183,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="1045,183,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="50"/>
    <Label Content="Logradouro:" HorizontalAlignment="Left" Margin="348,215,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <Label Content="Número:" HorizontalAlignment="Left" Margin="681,215,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <Label Content="Complemento:" HorizontalAlignment="Left" Margin="906,216,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="425,216,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="742,216,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="999,216,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="96"/>
    <Label Content="Cód. Serv.:" HorizontalAlignment="Left" Margin="405,246,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <Label Content="Cód. Ativ.:" HorizontalAlignment="Left" Margin="675,246,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <Label Content="Regime Trib.:" HorizontalAlignment="Left" Margin="391,278,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <Label Content="Tipo de Empresa:" HorizontalAlignment="Left" Margin="369,309,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="474,247,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="161"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="742,247,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="353"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="475,278,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="620"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="475,309,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="620"/>
    <Label Content="CNAE - Classificação Nacional de Atividades Econômicas" HorizontalAlignment="Left" Margin="130,336,0,0" Grid.Row="1" VerticalAlignment="Top" FontWeight="Bold"/>
    <Label Content="Código:" HorizontalAlignment="Left" Margin="79,362,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <Label Content="Descrição:" HorizontalAlignment="Left" Margin="320,362,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="133,362,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="389,362,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="605"/>
    <Button Content="Adicionar" HorizontalAlignment="Left" Margin="1006,362,0,0" Grid.Row="1" VerticalAlignment="Top" Width="90 "/>
    <DataGrid HorizontalAlignment="Left" Margin="133,393,0,0" Grid.Row="1" VerticalAlignment="Top" Width="962" Height="60"/>
    <Label Content="Descrição da Atividade:" HorizontalAlignment="Left" Margin="-2,456,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="130,456,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="965"/>
    <Label Content="Contatos" HorizontalAlignment="Left" Margin="126,483,0,0" Grid.Row="1" VerticalAlignment="Top" FontWeight="Bold"/>
    <Label Content="E-mail:" HorizontalAlignment="Left" Margin="82,509,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="130,510,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="350"/>
    <Label Content="Observação:" HorizontalAlignment="Left" Margin="517,509,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="597,510,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="397"/>
    <Button Content="Adicionar" HorizontalAlignment="Left" Margin="1006,510,0,0" Grid.Row="1" VerticalAlignment="Top" Width="90"/>
    <DataGrid HorizontalAlignment="Left" Margin="130,541,0,0" Grid.Row="1" VerticalAlignment="Top" Width="965" Height="60"/>
    <Label Content="Prefixo (99):" HorizontalAlignment="Left" Margin="56,606,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <Label Content="Telefone:" HorizontalAlignment="Left" Margin="233,606,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <Label Content="Observação:" HorizontalAlignment="Left" Margin="580,606,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="130,607,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="70"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="296,607,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="250"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="660,607,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="334"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="1006,606,0,0" Grid.Row="1" VerticalAlignment="Top" Width="90"/>
    <DataGrid HorizontalAlignment="Left" Margin="130,638,0,0" Grid.Row="1" VerticalAlignment="Top" Width="965" Height="58"/>
    <Button Content="Salvar" HorizontalAlignment="Left" Margin="133,731,0,0" Grid.Row="1" VerticalAlignment="Top" Width="92"/>
    </StackPanel>
        </ScrollViewer>
</Grid>

1 answer

2


Instead of using a Stackpanel and try to position the Label and Textbox through the use of Margin and Alignment, build your form using a Grid.

A form, in its simplest form, is nothing more than a list, where each line is a pair of elements Label -> Textbox

So use a Grid with two columns to define the form.
In the first column put the Label and on Monday the Textbox.
Each of the lines will then be a set Label -> Textbox.

Thus, it will only be necessary to define the row height and column width of the Grid so that everything is aligned.

That one Grid is placed inside the Scrollview:

<ScrollViewer VerticalScrollBarVisibility="Auto" Grid.RowSpan="2">
   <Grid x:Name="MyGrid" >
       <Grid.RowDefinitions>
           <RowDefinition Height="Auto"/>
           <RowDefinition Height="Auto"/>
           .....
           .....
           // Tantas quanto o número de pares *Label* -> *TextBox*
       </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
         <ColumnDefinition Width="Auto"/>
         <ColumnDefinition Width="*"/>
      </Grid.ColumnDefinitions>

      //Introduza aqui o Label indicado Grid.Column="0" e Grid.Row="0"
      //depois o TextBox indicado Grid.Column="1" e Grid.Row="0"
      //e assim sucessivamente, aumentando o valor de Grid.Row em cada par Label -> TextBox
   </Grid>        
</ScrollViewer>  
  • Can you give me an example of how to do that? Because before I put this Stackpanel, it was all okay. And I put it on myself just so I could have a Scroll...

  • I edited the answer.

  • Thanks again Ramaral. You are a genius! xD

Browser other questions tagged

You are not signed in. Login or sign up in order to post.