Scrollviewer to a postion of a control by code - wpf c# - c#

I am developing a small application when i need to scroll by code to a postion of a GroupBox Control, the part of my xaml files that contains the scrollviewer is the following one (I have a scrollviewer, inside a stackpanel and inside of that the GroupBox):
<ScrollViewer x:Name="scrollViewer" Grid.Row="1" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Visible">
<StackPanel x:Name="stackPanel" VerticalAlignment="Stretch" Margin="0,16,0,0">
<GroupBox x:Name="groupBox">
<GroupBox.Header>
<TextBlock Text="Desbloquear" FontWeight="Bold"/>
</GroupBox.Header>
<StackPanel Margin="0" Orientation="Vertical" Height="155" HorizontalAlignment="Stretch" VerticalAlignment="Top" Background="LightBlue">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Label Content="Usuario:" Grid.Row="0" Grid.Column="0" Margin="0,2,0,2"/>
<ComboBox x:Name="comboBoxUnblockUser" IsReadOnly="true" Margin="4,4,4,4" Grid.Row="0" Grid.Column="1"/>
<Label Content="Caja:" Margin="0,2,0,2" Grid.Row="1" Grid.Column="0"/>
<ComboBox x:Name="comboBoxUnblockCashMachine" Margin="4,4,4,4" IsReadOnly="true" Grid.Row="1" Grid.Column="1"/>
<Label Content="Estatus de solicitud:" Margin="0,2,0,2" Grid.Row="2" Grid.Column="0"/>
<Label x:Name="labelStatusUnblock" Margin="0,2,0,2" Content="----------" FontWeight="Bold" Grid.Row="2" Grid.Column="1"/>
<Label Content="Mensaje de Soporte:" Margin="0,2,0,2" Grid.Row="3" Grid.Column="0"/>
<Label x:Name="labelMessageUnblock" Margin="0,2,0,2" Content="----------" FontWeight="Bold" Grid.Row="3" Grid.Column="1"/>
</Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35"/>
</Grid.RowDefinitions>
<Button x:Name="buttonSolicitudeUnblock" Content="Solicitar" HorizontalAlignment="Left" Margin="4,0,0,0" VerticalAlignment="Top" Width="75" Height="30" ToolTip="Entrar a la aplicación"
Background="WhiteSmoke"
Foreground="Black"
FontFamily="Arial"
FontWeight="Bold" Click="buttonSolicitudeUnblock_Click"/>
<Button x:Name="buttonExecuteUnblock" Content="Ejecutar" IsEnabled="false" HorizontalAlignment="Left" Margin="90,0,0,0" VerticalAlignment="Top" Width="75" Height="30" ToolTip="Entrar a la aplicación"
Background="WhiteSmoke"
Foreground="Black"
FontFamily="Arial"
FontWeight="Bold"/>
</Grid>
</StackPanel>
</GroupBox>
<GroupBox x:Name="groupBox2">
<GroupBox.Header>
<TextBlock Text="Desbloquear caja Impresora Fiscal" FontWeight="Bold"/>
</GroupBox.Header>
<StackPanel Margin="0" Orientation="Vertical" Height="125" HorizontalAlignment="Stretch" VerticalAlignment="Top" Background="White">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Label Content="Caja:" Grid.Row="0" Grid.Column="0" Margin="0,2,0,2"/>
<ComboBox x:Name="comboBoxUnblockFiscalMachine" IsReadOnly="true" Margin="4,4,4,4" Grid.Row="0" Grid.Column="1"/>
<Label Content="Estatus de solicitud:" Margin="0,2,0,2" Grid.Row="1" Grid.Column="0"/>
<Label x:Name="labelStatusUnblockFiscalMachine" Margin="0,2,0,2" Content="----------" FontWeight="Bold" Grid.Row="1" Grid.Column="1"/>
<Label Content="Mensaje de Soporte:" Margin="0,2,0,2" Grid.Row="2" Grid.Column="0"/>
<Label x:Name="labelMessageUnblockFiscalMachine" Margin="0,2,0,2" Content="----------" FontWeight="Bold" Grid.Row="2" Grid.Column="1"/>
</Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35"/>
</Grid.RowDefinitions>
<Button x:Name="buttonSolicitudeUnblockFiscalMachine" Content="Solicitar" HorizontalAlignment="Left" Margin="4,0,0,0" VerticalAlignment="Top" Width="75" Height="30" ToolTip="Entrar a la aplicación"
Background="WhiteSmoke"
Foreground="Black"
FontFamily="Arial"
FontWeight="Bold"/>
<Button x:Name="buttonExecuteUnblockFiscalMachine" Content="Ejecutar" IsEnabled="false" HorizontalAlignment="Left" Margin="90,0,0,0" VerticalAlignment="Top" Width="75" Height="30" ToolTip="Entrar a la aplicación"
Background="WhiteSmoke"
Foreground="Black"
FontFamily="Arial"
FontWeight="Bold"/>
</Grid>
</StackPanel>
</GroupBox>
<GroupBox x:Name="groupBox3">
<GroupBox.Header>
<TextBlock Text="Reenvio de Cierre" FontWeight="Bold"/>
</GroupBox.Header>
<StackPanel Margin="0" Orientation="Vertical" Height="125" HorizontalAlignment="Stretch" VerticalAlignment="Top" Background="LightBlue">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Label Content="Fecha:" Grid.Row="0" Grid.Column="0" Margin="0,2,0,2"/>
<DatePicker x:Name="datePickerResendCloser" Margin="4,4,4,4" Grid.Row="0" Grid.Column="1" SelectedDate="{x:Static sys:DateTime.Now}" Focusable="False"/>
<Label Content="Estatus de solicitud:" Margin="0,2,0,2" Grid.Row="1" Grid.Column="0"/>
<Label x:Name="labelStatusResendCloser" Margin="0,2,0,2" Content="----------" FontWeight="Bold" Grid.Row="1" Grid.Column="1"/>
<Label Content="Mensaje de Soporte:" Margin="0,2,0,2" Grid.Row="2" Grid.Column="0"/>
<Label x:Name="labelMessageResendCloser" Margin="0,2,0,2" Content="----------" FontWeight="Bold" Grid.Row="2" Grid.Column="1"/>
</Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35"/>
</Grid.RowDefinitions>
<Button x:Name="buttonSolicitudeResendCloser" Content="Solicitar" HorizontalAlignment="Left" Margin="4,0,0,0" VerticalAlignment="Top" Width="75" Height="30" ToolTip="Entrar a la aplicación"
Background="WhiteSmoke"
Foreground="Black"
FontFamily="Arial"
FontWeight="Bold"/>
<Button x:Name="buttonExecuteResendCloser" Content="Ejecutar" IsEnabled="false" HorizontalAlignment="Left" Margin="90,0,0,0" VerticalAlignment="Top" Width="75" Height="30" ToolTip="Entrar a la aplicación"
Background="WhiteSmoke"
Foreground="Black"
FontFamily="Arial"
FontWeight="Bold"/>
</Grid>
</StackPanel>
</GroupBox>
<GroupBox x:Name="groupBox4">
<GroupBox.Header>
<TextBlock Text="Cerrar caja (cuando se asignó mas no se usó)" FontWeight="Bold"/>
</GroupBox.Header>
<StackPanel Margin="0" Orientation="Vertical" Height="155" HorizontalAlignment="Stretch" VerticalAlignment="Top" Background="White">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Label Content="Caja:" Grid.Row="0" Grid.Column="0" Margin="0,2,0,2"/>
<ComboBox x:Name="comboBoxCloseCashMachine" IsReadOnly="true" Margin="4,4,4,4" Grid.Row="0" Grid.Column="1"/>
<Label Content="Fecha:" Grid.Row="1" Grid.Column="0" Margin="0,2,0,2"/>
<DatePicker x:Name="datePickerCloseCashMachine" Margin="4,4,4,4" Grid.Row="1" Grid.Column="1" SelectedDate="{x:Static sys:DateTime.Now}" Focusable="False"/>
<Label Content="Estatus de solicitud:" Margin="0,2,0,2" Grid.Row="2" Grid.Column="0"/>
<Label x:Name="labelStatusCloseCashMachine" Margin="0,2,0,2" Content="----------" FontWeight="Bold" Grid.Row="2" Grid.Column="1"/>
<Label Content="Mensaje de Soporte:" Margin="0,2,0,2" Grid.Row="3" Grid.Column="0"/>
<Label x:Name="labelMessageCloseCashMachine" Margin="0,2,0,2" Content="----------" FontWeight="Bold" Grid.Row="3" Grid.Column="1"/>
</Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35"/>
</Grid.RowDefinitions>
<Button x:Name="buttonSolicitudeCloseCashMachine" Content="Solicitar" HorizontalAlignment="Left" Margin="4,0,0,0" VerticalAlignment="Top" Width="75" Height="30" ToolTip="Entrar a la aplicación"
Background="WhiteSmoke"
Foreground="Black"
FontFamily="Arial"
FontWeight="Bold"/>
<Button x:Name="buttonExecuteCloseCashMachine" Content="Ejecutar" IsEnabled="false" HorizontalAlignment="Left" Margin="90,0,0,0" VerticalAlignment="Top" Width="75" Height="30" ToolTip="Entrar a la aplicación"
Background="WhiteSmoke"
Foreground="Black"
FontFamily="Arial"
FontWeight="Bold"/>
</Grid>
</StackPanel>
</GroupBox>
</StackPanel>
</ScrollViewer>
I have tried other solutions in stackoverflow but i can't figure out why i can not handled it to work.
I just have the declaration of the function
private void ScrollToGroupBox(System.Windows.Controls.GroupBox groupBox)
{
//I NEED TO GET THE POSITION OF THE GRUOPBOX INSIDE OF THE SCROLLVIEWER
scrollViewer.ScrollToVerticalOffset(0);
}

private void ScrollToGroupBox(System.Windows.Controls.GroupBox groupBox)
{
groupBox.BringIntoView();
}

Related

Getting an error called 'Windows.UI.Xaml.Markup.XamlParseException' [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm getting this error while trying to develop my app, the compilator said that it could not find an error code. So I don't have more information. I know that the error is generated while trying to initializeComponent.
Here is the code of my page:
<RelativePanel Padding="40">
<Image x:Name="logo" Source="ms-appx:///Assets/UPSA.jpg" Width="150" Height="200"/>
<Border RelativePanel.RightOf="logo" Height="200" RelativePanel.AlignRightWithPanel="True" Background="Orange" UseLayoutRounding="True" CornerRadius="40,40,40,40" Margin="30,0,0,0" />
<TextBlock Text="Universidad Pontifica de Salamanca" FontSize="72" RelativePanel.RightOf="logo" RelativePanel.AlignRightWithPanel="True" HorizontalAlignment="Center" FontFamily="Cooper Black" RelativePanel.AlignVerticalCenterWith="logo"/>
<Grid Margin="30" x:Name="mainGridL" RelativePanel.Below="logo" Height="500" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignLeftWithPanel="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1.3*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<RelativePanel Grid.Column="0" >
<TextBlock x:Name="textPersonal" Text="Datos Personales" FontSize="60" VerticalAlignment="Top" HorizontalAlignment="Center" Foreground="Orange"/>
<Line x:Name="lineBText" Stroke="Black" RelativePanel.Below="textPersonal" Width="770" StrokeThickness="20" X1="0" X2="770"/>
<Line x:Name="lineMid" Stroke="Black" RelativePanel.Below="textPersonal" Width="770" StrokeThickness="20" X1="770" X2="770" Y1="0" Y2="420" />
<Grid RelativePanel.Below="lineBText" Margin="0,20,0,0" RelativePanel.AlignBottomWithPanel="True" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignLeftWithPanel="True">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="0.5*"/>
<RowDefinition Height="0.5*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" x:Name="textDni" Text="Dni" FontSize="35" VerticalAlignment="Center" Margin="30,0,0,0"/>
<TextBlock Grid.Row="0" Grid.Column="1" x:Name="textEmail" Text="Email" FontSize="35" VerticalAlignment="Center" Margin="30,0,0,0" />
<TextBox Grid.Row="1" Grid.Column=" 0" x:Name="nifBox" PlaceholderText="DNI..." FontSize="20" Background="Gray" VerticalAlignment="Center" Margin="29,0,30,0" />
<TextBox x:Name="emailBox" Grid.Row="1" Grid.Column="1" PlaceholderText="Email..." FontSize="20" Background="Gray" VerticalAlignment="Center" Margin="30,0,30,0" />
<TextBlock Grid.Row="2" Grid.Column="0" Text="Nombre" FontSize="35" VerticalAlignment="Center" Margin="30,0,0,0"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="Apellidos" FontSize="35" VerticalAlignment="Center" Margin="30,0,0,0"/>
<TextBox x:Name="nameBox" Grid.Row="3" Grid.Column=" 0" PlaceholderText="Nombre..." FontSize="20" Background="Gray" VerticalAlignment="Center" Margin="30,0,30,0" />
<TextBox x:Name="surnameBox" Grid.Row="3" Grid.Column="1" PlaceholderText="Apellido..." FontSize="20" Background="Gray" VerticalAlignment="Center" Margin="30,0,30,0" />
<TextBlock Grid.Row="4" Grid.Column="0" Text="Genero" FontSize="35" Grid.ColumnSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<RadioButton Grid.Row="5" Grid.Column="0" x:Name="genderMale" Content="Hombre" FontSize="20" HorizontalAlignment="Center" Grid.ColumnSpan="2"/>
<RadioButton Grid.Row="6" Grid.Column="0" x:Name="genderFemale" Content="Mujer" FontSize="20" HorizontalAlignment="Center" Grid.ColumnSpan="2"/>
</Grid>
</RelativePanel>
<RelativePanel Grid.Column="1">
<TextBlock x:Name="textIns" Text="Datos Inscripcion" FontSize="60" VerticalAlignment="Top" HorizontalAlignment="Center" Foreground="Orange"/>
<Line x:Name="lineBTextIns" Stroke="Black" RelativePanel.Below="textIns" Width="770" StrokeThickness="20" X1="0" X2="770"/>
<StackPanel RelativePanel.Below="lineBTextIns" Orientation="Vertical" RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignBottomWithPanel="True" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock Text="Titulacion cursada" FontSize="40" Margin="0,0,0,30"/>
<ComboBox x:Name="degreeName" PlaceholderText="Esoge tu titulacion" HorizontalAlignment="Center">
<x:String>ADET</x:String>
<x:String>Ing.Informatica</x:String>
<x:String>ADET + Ing.Informatica</x:String>
<x:String>Ing.Informatica + ADET</x:String>
</ComboBox>
<TextBlock Text="Escoge una charla" FontSize="40" Margin="0,0,0,30"/>
<ComboBox x:Name="activities" PlaceholderText="Escoge una charla" HorizontalAlignment="Center" ItemsSource="{Binding Name}"/>
</StackPanel>
</RelativePanel>
</Grid>
<Button RelativePanel.Below="mainGridL" Content="Enviar" FontSize="40" Margin="0,21,0,0" RelativePanel.AlignHorizontalCenterWithPanel="True" />
</RelativePanel>
After a long time of trying and error, I found where is the issue located. In the deepest Grid, the error is generated while trying to create TextBox, don't know why because if you just delete textBox it works properly.
Here is the error...
<Grid RelativePanel.Below="lineBText" Margin="0,20,0,0" RelativePanel.AlignBottomWithPanel="True" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignLeftWithPanel="True">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="0.5*"/>
<RowDefinition Height="0.5*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" x:Name="textDni" Text="Dni" FontSize="35" VerticalAlignment="Center" Margin="30,0,0,0"/>
<TextBlock Grid.Row="0" Grid.Column="1" x:Name="textEmail" Text="Email" FontSize="35" VerticalAlignment="Center" Margin="30,0,0,0" />
<TextBox Grid.Row="1" Grid.Column=" 0" x:Name="nifBox" PlaceholderText="DNI..." FontSize="20" Background="Gray" VerticalAlignment="Center" Margin="29,0,30,0" />
<TextBox x:Name="emailBox" Grid.Row="1" Grid.Column="1" PlaceholderText="Email..." FontSize="20" Background="Gray" VerticalAlignment="Center" Margin="30,0,30,0" />
<TextBlock Grid.Row="2" Grid.Column="0" Text="Nombre" FontSize="35" VerticalAlignment="Center" Margin="30,0,0,0"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="Apellidos" FontSize="35" VerticalAlignment="Center" Margin="30,0,0,0"/>
<TextBox x:Name="nameBox" Grid.Row="3" Grid.Column=" 0" PlaceholderText="Nombre..." FontSize="20" Background="Gray" VerticalAlignment="Center" Margin="30,0,30,0" />
<TextBox x:Name="surnameBox" Grid.Row="3" Grid.Column="1" PlaceholderText="Apellido..." FontSize="20" Background="Gray" VerticalAlignment="Center" Margin="30,0,30,0" />
<TextBlock Grid.Row="4" Grid.Column="0" Text="Genero" FontSize="35" Grid.ColumnSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<RadioButton Grid.Row="5" Grid.Column="0" x:Name="genderMale" Content="Hombre" FontSize="20" HorizontalAlignment="Center" Grid.ColumnSpan="2"/>
<RadioButton Grid.Row="6" Grid.Column="0" x:Name="genderFemale" Content="Mujer" FontSize="20" HorizontalAlignment="Center" Grid.ColumnSpan="2"/>
</Grid>
Thanks for helping.
Pay attention to this two lines:
<TextBox Grid.Row="1" Grid.Column=" 0" x:Name="nifBox" PlaceholderText="DNI..." FontSize="20" Background="Gray" VerticalAlignment="Center" Margin="29,0,30,0" />
<TextBox x:Name="nameBox" Grid.Row="3" Grid.Column=" 0" PlaceholderText="Nombre..." FontSize="20" Background="Gray" VerticalAlignment="Center" Margin="30,0,30,0" />
You have an extra space before 0 value in the Grid.Column. You need to pass "0" instead of " 0".

C# WPF Scrollbar changes/moves content

I'm having a hard time making my WPF form scrollable. The form is quite long, and i want the user to be able to scroll further down. If i adjust the height of the form, as the scrollbar becomes active, it moves everything around, and makes my content smaller. How do i avoid this? I've added 2 Pictures (Before & After) and my XAML code.
<Window x:Class="RegisterForm.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1000" Width="700" MinHeight="1000" MinWidth="700">
<ScrollViewer
VerticalScrollBarVisibility="Visible"
CanContentScroll="True">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label
Content="Full Name"
FontSize="14"
FontWeight="Bold"
Grid.Row="1"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Gender"
FontSize="14"
FontWeight="Bold"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Date of Birth"
FontSize="14"
FontWeight="Bold"
Grid.Row="3"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Address"
FontSize="14"
FontWeight="Bold"
Grid.Row="4"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Phone Number"
FontSize="14"
FontWeight="Bold"
Grid.Row="7"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Phone Number Family"
FontSize="14"
FontWeight="Bold"
Grid.Row="8"
Grid.ColumnSpan="3"
Margin="40,0,0,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="E-mail"
FontSize="14"
FontWeight="Bold"
Grid.Row="9"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Do you speak english?"
FontSize="14"
FontWeight="Bold"
Grid.Row="10"
Grid.ColumnSpan="3"
Margin="40,0,0,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Education"
FontSize="14"
FontWeight="Bold"
Grid.Row="11"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Job Type/Profession"
FontSize="14"
FontWeight="Bold"
Grid.Row="12"
Grid.ColumnSpan="3"
Margin="40,0,0,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Experience"
FontSize="14"
FontWeight="Bold"
Grid.Row="13"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Drivers License"
FontSize="14"
FontWeight="Bold"
Grid.Row="14"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<TextBox
x:Name="txtFirstName"
FontSize="14"
Grid.Row="1"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="First Name"
FontSize="12"
Grid.Row="1"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<TextBox
x:Name="txtLastName"
FontSize="14"
Grid.Row="1"
Grid.Column="5"
Grid.ColumnSpan="3"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="Last Name"
FontSize="12"
Grid.Row="1"
Grid.Column="5"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<ComboBox
x:Name="CBGender"
FontSize="12"
Grid.Row="2"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,0,0,30"
IsEditable="True"
IsReadOnly="True"
Text="Please Select"
FontStyle="Normal">
<ListBoxItem Content="Male" FontStyle="Normal"/>
<ListBoxItem Content="Female" FontStyle="Normal"/>
</ComboBox>
<ComboBox
x:Name="CBDOBDay"
FontSize="12"
Grid.Row="3"
Grid.Column="3"
Grid.ColumnSpan="1"
Margin="10,0,0,30"
IsEditable="True"
IsReadOnly="True"
Text="DD"
FontStyle="Normal">
</ComboBox>
<ComboBox
x:Name="CBDOBMonth"
FontSize="12"
Grid.Row="3"
Grid.Column="4"
Grid.ColumnSpan="1"
Margin="10,0,0,30"
IsEditable="True"
IsReadOnly="True"
Text="MM"
FontStyle="Normal">
</ComboBox>
<ComboBox
x:Name="CBDOBYear"
FontSize="12"
Grid.Row="3"
Grid.Column="5"
Grid.ColumnSpan="1"
Margin="10,0,0,30"
IsEditable="True"
IsReadOnly="True"
Text="YY"
FontStyle="Normal">
</ComboBox>
<TextBox
x:Name="txtStreetAddress"
FontSize="14"
Grid.Row="4"
Grid.Column="3"
Grid.ColumnSpan="3"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="Street Address"
FontSize="12"
Grid.Row="4"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<TextBox
x:Name="txtCity"
FontSize="14"
Grid.Row="5"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="City"
FontSize="12"
Grid.Row="5"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<TextBox
x:Name="txtStateProvince"
FontSize="14"
Grid.Row="5"
Grid.Column="5"
Grid.ColumnSpan="3"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="State / province"
FontSize="12"
Grid.Row="5"
Grid.Column="5"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<TextBox
x:Name="txtPostalZipCode"
FontSize="14"
Grid.Row="6"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="Postal / Zip code"
FontSize="12"
Grid.Row="6"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<ComboBox
x:Name="CBCountry"
FontSize="12"
Grid.Row="6"
Grid.Column="5"
Grid.ColumnSpan="3"
Margin="10,0,0,30"
IsEditable="True"
IsReadOnly="True"
Text="Please Select"
FontStyle="Normal" SelectionChanged="CBCountry_SelectionChanged">
</ComboBox>
<Label
Content="Country"
FontSize="12"
Grid.Row="6"
Grid.Column="5"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<TextBox
x:Name="txtPhone1AreaCode"
FontSize="14"
Grid.Row="7"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<TextBox
x:Name="txtPhone1"
FontSize="14"
Grid.Row="7"
Grid.Column="5"
Grid.ColumnSpan="3"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="Area Code (ex. +45)"
FontSize="12"
Grid.Row="7"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<Label
Content="Phone Number"
FontSize="12"
Grid.Row="7"
Grid.Column="5"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<TextBox
x:Name="txtPhone2AreaCode"
FontSize="14"
Grid.Row="8"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<TextBox
x:Name="txtPhone2"
FontSize="14"
Grid.Row="8"
Grid.Column="5"
Grid.ColumnSpan="3"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="Area Code (ex. +45)"
FontSize="12"
Grid.Row="8"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<Label
Content="Phone Number"
FontSize="12"
Grid.Row="8"
Grid.Column="5"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<TextBox
x:Name="txtEmail"
FontSize="14"
Grid.Row="9"
Grid.Column="3"
Grid.ColumnSpan="4"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="Ex. myname#example.com"
FontSize="12"
Grid.Row="9"
Grid.Column="3"
Grid.ColumnSpan="3"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
</Grid>
</ScrollViewer>
//Thanks, Rasmus
The problem with ScrollViewer is that it is virtually infinite. So, the content inside it can't calculate relative sizes such as *. In your case, i think if you add a height to your grid, something like <Grid Height="1000"> it will work fine.
You have to specify the Heigh or MinHeigh of at least one part: The Editors (Labels / Textboxes / Comboboxes) or the GridRows or the whole Grid. Currrently the Grid is filled within the Window and the Columns also fill all together the whole Grid. Normally you would Set Grids' Rows Height to
<RowDefinition Height="auto" />
instead of
<RowDefinition Height="*" />
and give the editors some kind of MinHeight or Padding.
So there are many Solutions. It just depends on how you want to have the Labels/Editors Height. (Dynamic or fixed)
EDIT/ADD:
And just as a SideNote, if you define the same style foreach of those Row-/ and ColumnDefinitions, you maybe should assign a Style in the Grid for those.
It makes things easier to Change at once ;D
<Grid>
<Grid.Resources>
<Style TargetType="RowDefinition">
<Setter Property="Height" Value="auto" />
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<!-- ... -->
</Grid>

C# WPF Dynamic created Labels in a Grid dont show on the Top of the Grid

Hi (sry for bad english) i have the problem that i create dynamic rows with label content in a grid but they dont begin at the top of the Grid.
private void SelectAccounts(string AccountUsername) //Findet Nutzer und Account ID und gibt alle Accounts des Nutzers aus //// neu machen jede reihe einzeln zurückgeben!!!!!!!
{
MId=database.GetID(this.MasterUsername,"MasterUser","MId"); //for database request
AccId = database.GetID(AccountUsername, "Accounts","AccId"); //for database request
AccountsList.Children.Clear(); //My Grid where i safe it
int rows = database.GetRows("Accounts", "AccID"); //How many Rows i have in my table
for (int i = 1; i <= rows; i++)
{
stringusername=database.GetUsernameById("Accounts","Username", i, MId); //getting the username back as string
if(username!="") // not evry username has the right condition so somtimes the function returns me only ""
{
Label l = new Label();
RowDefinition newRow = new RowDefinition();
newRow.Height = new GridLength(40);
AccountsList.RowDefinitions.Add(newRow);
l.Content = username;
AccountsList.Children.Add(l);
l.SetValue(Grid.RowProperty, i);
}
}
}
the problem is when i now start the programm it looks like this:
enter image description here
if i use the loop like this:
for (int i = 1; i <= rows; i++)
{
Label l = new Label();
RowDefinition newRow = new RowDefinition();
newRow.Height = new GridLength(40);
AccountsList.RowDefinitions.Add(newRow);
string username= database.GetUsernameById("Accounts", "Username", i, MId);
if(username!="")
{
l.Content = username;
AccountsList.Children.Add(l);
l.SetValue(Grid.RowProperty, i);
}
}
and it looks like this: enter image description here
I can normally add a label in the .xaml and it show normal on the top of the grid.
MyUserControll where i want to show the rows:
<UserControl x:Class="AccountManager.Login"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="750">
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="400"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="13*"/>
<ColumnDefinition Width="37*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Row="0" Grid.Column="0">
<Label Content="Your Accounts" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="21.333" FontFamily="Tekton Pro" FontWeight="Bold"></Label>
<Border BorderBrush="LightBlue" BorderThickness="1.5" HorizontalAlignment="Right"/>
<Border BorderBrush="LightBlue" BorderThickness="1.5" VerticalAlignment="Bottom"/>
<Border BorderBrush="Blue" BorderThickness="1.5" VerticalAlignment="Bottom" Opacity="0.2"/>
<Border BorderBrush="Blue" BorderThickness="1.5" HorizontalAlignment="Right" Opacity="0.2"/>
</Grid>
<Grid Grid.Row="1" Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="146*"/>
<ColumnDefinition Width="348*"/>
<ColumnDefinition Width="91*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Row="0" Grid.Column="0">
<Border BorderBrush="LightBlue" BorderThickness="1.5" HorizontalAlignment="Right"/>
<Border BorderBrush="Blue" BorderThickness="1.5" Opacity="0.2" HorizontalAlignment="Right"/>
</Grid>
<Grid Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="110*"/>
<ColumnDefinition Width="191*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Label Content="Benutzername:" VerticalAlignment="Center" HorizontalAlignment="Right" Height="26" Width="90"/>
<TextBox x:Name="addBenutzername" Grid.Row="0" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Right" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1" />
<PasswordBox x:Name="addPw" Grid.Row="1" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Right" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1" ></PasswordBox>
<Label Grid.Row="1" Grid.Column="0" Content="Password:" VerticalAlignment="Center" HorizontalAlignment="Right" Height="26" Width="62"/>
<Label x:Name="repeatPasswordLabel" Grid.Row="2" Grid.Column="0" Content="Repeat Password:" HorizontalAlignment="Right" VerticalAlignment="Center" Height="26" Width="102" />
<PasswordBox x:Name="addReapeatPw" Grid.Row="2" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Right" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1" />
<Label Content="Email:" Grid.Row="3" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right" Height="26" Width="41"/>
<TextBox x:Name="addEmail" Grid.Row="3" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Right" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1"/>
<Button Grid.Row=" 5" Grid.Column="1" Content="ADD" Height="36" HorizontalAlignment="Center" VerticalAlignment="Center" Width="80" Click="AddAccount"></Button>
</Grid>
<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<RadioButton x:Name="r1" Grid.Row="0" Width="70" Height="30">
<Image Source="Bilder/Facebook.png"></Image>
</RadioButton>
<RadioButton x:Name="r2" Grid.Row="1" Width="70" Height="30">
<Image Source="Bilder/Skype.png"></Image>
</RadioButton>
<RadioButton x:Name="r3" Grid.Row="2" Width="70" Height="30">
<Image Source="Bilder/Youtube.png"></Image>
</RadioButton>
<RadioButton x:Name="r4" Grid.Row="3" Width="70" Height="30">
<Image Source="Bilder/Twitter.png"></Image>
</RadioButton>
<RadioButton x:Name="r5" Grid.Row="4" Width="70" Height="30">
<Image Source="Bilder/Microsoft.png"></Image>
</RadioButton>
<RadioButton x:Name="r6" Grid.Row="5" Width="70" Height="30">
<Image Source="Bilder/reddit.png"></Image>
</RadioButton>
<RadioButton x:Name="r7" Grid.Row="6" Width="70" Height="30">
<Image Source="Bilder/Origin.png"></Image>
</RadioButton>
<RadioButton x:Name="r8" Grid.Row="8" Width="70" Height="30">
<Image Source="Bilder/Steam.png"></Image>
</RadioButton>
</Grid>
</Grid>
<Grid VerticalAlignment="Top" x:Name="AccountsList" Grid.Row="1" Grid.Column="0">
<Border BorderBrush="LightBlue" BorderThickness="1.5" HorizontalAlignment="Right"/>
<Border BorderBrush="Blue" BorderThickness="1.5" HorizontalAlignment="Right" Opacity="0.2"/>
</Grid>
<Grid Grid.Row="0" Grid.Column="1">
<Border BorderBrush="LightBlue" BorderThickness="1.5" VerticalAlignment="Bottom"/>
<Border BorderBrush="Blue" BorderThickness="1.5" VerticalAlignment="Bottom" Opacity="0.2"/>
<Button x:Name="delete" Content="Delete" HorizontalAlignment="Left" Width="80" Height="36" Margin="85,19,0,19"/>
<Button x:Name="change" Content="Change" HorizontalAlignment="Center" VerticalAlignment="Center" Width="80" Height="36"></Button>
</Grid>
</Grid>
My Main Windows is:
<Window x:Class="AccountManager.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="750" MaxWidth="750" MinWidth="750" MinHeight="500" MaxHeight="500">
<Grid x:Name="Main">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="371"></ColumnDefinition>
<ColumnDefinition Width="371"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="80"></RowDefinition>
<RowDefinition Height="190"></RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="Accountmanager" HorizontalAlignment="Center" VerticalAlignment="Center" Background="White" FontSize="24" FontFamily="Snap ITC"/>
<Grid Grid.Row="0" Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
</Grid>
<Grid Grid.Row="1" x:Name="bilder">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Source="Bilder/Facebook.png" Height="70" HorizontalAlignment="Center" VerticalAlignment="Center" Width="70"/>
<Image Grid.Row="0" Grid.Column="1" Source="Bilder/Skype.png" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Image Grid.Row="0" Grid.Column="2" Source="Bilder/Youtube.png" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Image Grid.Row="0" Grid.Column="3" Source="Bilder/Twitter.png" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Image Grid.Row="1" Grid.Column="0" Source="Bilder/Microsoft.png" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Image Grid.Row="1" Grid.Column="1" Source="Bilder/reddit.png" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
<Image Grid.Row="1" Grid.Column="2" Source="Bilder/Origin.png" VerticalAlignment="Center" HorizontalAlignment="Center"></Image>
<Image Grid.Row="1" Grid.Column="3" Source="Bilder/Steam.png" VerticalAlignment="Center" HorizontalAlignment="Center"></Image>
</Grid>
<Grid Grid.Row="1" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Content="Benutzername:" VerticalAlignment="Top" HorizontalAlignment="Right"/>
<TextBox x:Name="benutzername" Grid.Row="0" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Black" BorderThickness="1"></TextBox>
<PasswordBox x:Name="pw" Grid.Row="0" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Left" BorderBrush="Black" BorderThickness="1" VerticalAlignment="Bottom"/>
<Label Grid.Row="0" Grid.Column="0" Content="Password:" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
<Button Grid.Column="0" Grid.Row="1" Content="Add" Width="80" Height="40" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="47,0,23,0" Click="ClickAddAccount"/>
<Button Grid.Column="1" Grid.Row="1" Content="Login" Width="80" Height="40" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,23,0" Click="Login" />
<Button Grid.Column="1" Grid.Row="1" Content="Forgot Password" HorizontalAlignment="Left" VerticalAlignment="Bottom" Height="40" Width="95" Click="ForgotPw" />
</Grid>
</Grid>
</Grid>
In your place, I would use an ItemsControl and create an object to bind the ItemsSource to. Create an ItemsControl like this:
<ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Window},
Path=Header}">//Instead of header, use your own sourceobject
<ItemsControl.ItemTemplate>
<DataTemplate>
//Insert your Itemtemplate here, you can bind to any object in the ItemsSource!
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
This prevents you from the hassle to create each and every control manually and you can leave the creation of the controls to the wpf!

Divide first row in the grid to four equal parts WPF

I used to draw something like tabcontrol in WizardPage control of extended WPF toolkit.
Relevant xaml code:
<xctk:WizardPage x:Name="Page1" PageType="Blank" Width="540"
BorderBrush="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<DockPanel Grid.Row="0">
<TextBox Width="135" Text="Step 1" Background="#FF2BADDE" FontSize="16" TextAlignment="Center" />
<TextBox Width="135" Text="Step 2" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
<TextBox Width="135" Text="Step 3" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
<TextBox Width="135" Text="Step 4" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
</DockPanel>
<Grid Grid.Row="1">
<GroupBox Header="Group 1" FontSize="16" Height="80" Margin="0,0,0,90" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="270"/>
<ColumnDefinition Width="270"/>
</Grid.ColumnDefinitions>
<RadioButton x:Name="RadioButNew" Content="New" FontSize="13.333" BorderThickness="0,1,1,1" HorizontalAlignment="Left" Margin="30,30,0,0"/>
<RadioButton x:Name="RadioButUpdate" Content="Update" Grid.Column="1" FontSize="13.333" Focusable="False" HorizontalAlignment="Left" Margin="30,30,0,0"/>
</Grid>
</GroupBox>
</Grid>
But text boxes inside grid aren't at the same width though I set each of them to same width which is the total WizardPage width/4 (since I have four text boxes).
Any solution please?
Thanks!
To equally divide in 4 columns , you should use a grid, instead of a DockPanel. Although it is unnecessary to precide the Width. Just indicate that you have 4 columns :
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="Step 1" Background="#FF2BADDE" FontSize="16" TextAlignment="Center" />
<TextBox Grid.Column="1" Text="Step 2" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
<TextBox Grid.Column="2" Text="Step 3" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
<TextBox Grid.Column="3" Text="Step 4" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
</Grid>
<Grid Grid.Row="1">
<GroupBox Header="Group 1" FontSize="16" Height="80" Margin="0,0,0,90" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="270"/>
<ColumnDefinition Width="270"/>
</Grid.ColumnDefinitions>
<RadioButton x:Name="RadioButNew" Content="New" FontSize="13.333" BorderThickness="0,1,1,1" HorizontalAlignment="Left" Margin="30,30,0,0"/>
<RadioButton x:Name="RadioButUpdate" Content="Update" Grid.Column="1" FontSize="13.333" Focusable="False" HorizontalAlignment="Left" Margin="30,30,0,0"/>
</Grid>
</GroupBox>
</Grid>
</Grid>
you can also simplify you xaml by using only 1 grid. Grid.ColumnSpan is handy is such a case :
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="80"/>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox Grid.Row="0" Grid.Column="0" Text="Step 1" Background="#FF2BADDE" FontSize="16" TextAlignment="Center" />
<TextBox Grid.Row="0" Grid.Column="1" Text="Step 2" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
<TextBox Grid.Row="0" Grid.Column="2" Text="Step 3" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
<TextBox Grid.Row="0" Grid.Column="3" Text="Step 4" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
<GroupBox Header="Group 1" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="4" FontSize="16" Margin="0,0,0,90" />
<RadioButton Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" x:Name="RadioButNew" Content="New" FontSize="13.333" BorderThickness="0,1,1,1" HorizontalAlignment="Center" Margin="30,30,0,0"/>
<RadioButton Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2" x:Name="RadioButUpdate" Content="Update" FontSize="13.333" Focusable="False" HorizontalAlignment="Center" Margin="30,30,0,0"/>
</Grid>

ScrollViewer is displaying but not moving in wpf?

I am having a problem that I want to include an ScrollViewer in my wpf application I used ScrollViewer property but it is just showing ScrollBar on the right side but it do not allow me to move.
My view gets down the Window but I do not able to see that. My code is:
<ScrollViewer >
<StackPanel Orientation="Vertical" ScrollViewer.CanContentScroll="True">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="170"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="65"/>
<!--<ColumnDefinition Width="205"/>-->
</Grid.ColumnDefinitions>
<Label
Grid.Row="0"
Grid.ColumnSpan="1"
HorizontalAlignment="Left"
Content="IP Configuration:"
Foreground="Black"
FontWeight="ExtraBold"
FontSize="14"
Opacity="0.8"
/>
<Label
Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Center"
Content="Wireless IP Address"
Foreground="Black"
Opacity="0.8"
/>
<TextBox
Grid.Row="1"
Grid.Column="1"
VerticalAlignment="Center"
/>
<Label
Grid.Row="2"
Grid.ColumnSpan="1"
HorizontalAlignment="Left"
Content="AP Group Configuration"
Foreground="Black"
FontWeight="ExtraBold"
FontSize="14"
Opacity="0.8"
/>
<Label
Grid.Row="3"
Grid.Column="0"
VerticalAlignment="Center"
Content="Group ID"
Foreground="Black"
Opacity="0.8"
Margin="0,0,0,5"
/>
<TextBox
Grid.Row="3"
Grid.Column="1"
VerticalAlignment="Center"
Margin="0,0,0,5"
/>
<Label
Grid.Row="3"
Grid.Column="2"
VerticalAlignment="Center"
Content="(1-1024)"
Foreground="Black"
Opacity="0.8"
Margin="0,0,0,5"
/>
<Label
Grid.Row="4"
Grid.Column="0"
VerticalAlignment="Center"
Content="AP Hardware Type"
Foreground="Black"
Opacity="0.8"
/>
<ComboBox
Grid.Row="4"
Grid.Column="1"
>
<ComboBoxItem >0-Any</ComboBoxItem>
<ComboBoxItem>21-ARC1000MAP , Indoor Radio b/g/n</ComboBoxItem>
<ComboBoxItem>22-ARC2000MAP , Dual Radio a/n, b/g/n</ComboBoxItem>
</ComboBox>
<Button
Grid.Row="4"
Grid.Column="2"
Margin="10,0,0,0"
Style="{StaticResource AppButtons}">
Add
</Button>
<TextBox
Grid.Row="5"
Grid.ColumnSpan="3"
IsReadOnly="True"
IsEnabled="False"
Visibility="Hidden"
Height="10"/>
<DataGrid Grid.Row="6"
Grid.ColumnSpan="3"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Width="100" Header="Group ID" />
<DataGridTextColumn Header="AP Hardware Type"/>
</DataGrid.Columns>
</DataGrid>
<Label
Grid.Row="7"
Grid.Column="0"
HorizontalAlignment="Left"
Content="Network Configuration"
FontWeight="ExtraBold"
Foreground="Black"
FontSize="14"
Opacity="0.8"
/>
<Label
Grid.Row="8"
Grid.Column="0"
VerticalAlignment="Center"
Content="SSID"
Foreground="Black"
Opacity="0.8"
/>
<TextBox
Grid.Row="8"
Grid.Column="1"
VerticalAlignment="Center"
Text="Guest Network"
/>
<Label
Grid.Row="9"
Grid.Column="0"
VerticalAlignment="Center"
Content="Security"
Foreground="Black"
Opacity="0.8"
/>
<ComboBox
Grid.Row="9"
Grid.Column="1"
ItemsSource="{Binding ACAvailableSecurityTypes}"
SelectedItem="{Binding ACSelectedSecurityType}"
>
</ComboBox>
<view:ACSecurityStaticWEP
Grid.Row="10"
Grid.ColumnSpan="2"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Visibility="{Binding IsACStaticWep, Converter={StaticResource BooleanToVisibilityConverter}}"/>
</Grid>
</StackPanel>
</ScrollViewer>
Your ScrollViewer height is infinite, set it to any value and that should do it
You need to set VerticalScrollBarVisibility of ScrollViewer
<ScrollViewer VerticalScrollBarVisibility="Auto >
Also your StackPanel Height property Should have some value
<StackPanel Height="500" Width="376" Orientation="Vertical" >
Complete Code:
<ScrollViewer VerticalScrollBarVisibility="Auto" >
<StackPanel Height="700" Orientation="Vertical" ScrollViewer.CanContentScroll="True">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="170"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="65"/>
<!--<ColumnDefinition Width="205"/>-->
</Grid.ColumnDefinitions>
<Label
Grid.Row="0"
Grid.ColumnSpan="1"
HorizontalAlignment="Left"
Content="IP Configuration:"
Foreground="Black"
FontWeight="ExtraBold"
FontSize="14"
Opacity="0.8"
/>
<Label
Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Center"
Content="Wireless IP Address"
Foreground="Black"
Opacity="0.8"
/>
<TextBox
Grid.Row="1"
Grid.Column="1"
VerticalAlignment="Center"
/>
<Label
Grid.Row="2"
Grid.ColumnSpan="1"
HorizontalAlignment="Left"
Content="AP Group Configuration"
Foreground="Black"
FontWeight="ExtraBold"
FontSize="14"
Opacity="0.8"
/>
<Label
Grid.Row="3"
Grid.Column="0"
VerticalAlignment="Center"
Content="Group ID"
Foreground="Black"
Opacity="0.8"
Margin="0,0,0,5"
/>
<TextBox
Grid.Row="3"
Grid.Column="1"
VerticalAlignment="Center"
Margin="0,0,0,5"
/>
<Label
Grid.Row="3"
Grid.Column="2"
VerticalAlignment="Center"
Content="(1-1024)"
Foreground="Black"
Opacity="0.8"
Margin="0,0,0,5"
/>
<Label
Grid.Row="4"
Grid.Column="0"
VerticalAlignment="Center"
Content="AP Hardware Type"
Foreground="Black"
Opacity="0.8"
/>
<ComboBox
Grid.Row="4"
Grid.Column="1"
>
<ComboBoxItem >0-Any</ComboBoxItem>
<ComboBoxItem>21-ARC1000MAP , Indoor Radio b/g/n</ComboBoxItem>
<ComboBoxItem>22-ARC2000MAP , Dual Radio a/n, b/g/n</ComboBoxItem>
</ComboBox>
<Button
Grid.Row="4"
Grid.Column="2"
Margin="10,0,0,0">
</Button>
<TextBox
Grid.Row="5"
Grid.ColumnSpan="3"
IsReadOnly="True"
IsEnabled="False"
Visibility="Hidden"
Height="10"/>
<DataGrid Grid.Row="6"
Grid.ColumnSpan="3"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Width="100" Header="Group ID" />
<DataGridTextColumn Header="AP Hardware Type"/>
</DataGrid.Columns>
</DataGrid>
<Label
Grid.Row="7"
Grid.Column="0"
HorizontalAlignment="Left"
Content="Network Configuration"
FontWeight="ExtraBold"
Foreground="Black"
FontSize="14"
Opacity="0.8"
/>
<Label
Grid.Row="8"
Grid.Column="0"
VerticalAlignment="Center"
Content="SSID"
Foreground="Black"
Opacity="0.8"
/>
<TextBox
Grid.Row="8"
Grid.Column="1"
VerticalAlignment="Center"
Text="Guest Network"
/>
<Label
Grid.Row="9"
Grid.Column="0"
VerticalAlignment="Center"
Content="Security"
Foreground="Black"
Opacity="0.8"
/>
<ComboBox
Grid.Row="9"
Grid.Column="1"
ItemsSource="{Binding ACAvailableSecurityTypes}"
SelectedItem="{Binding ACSelectedSecurityType}"
>
</ComboBox>
</Grid>
</StackPanel>
</ScrollViewer>

Categories