Home > front end >  Scrollviewer to a postion of a control by code - wpf c#
Scrollviewer to a postion of a control by code - wpf c#

Time:06-14

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);
}

CodePudding user response:

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