[英]Scrollviewer to a postion of a control by code - wpf c#
当我需要通过代码滚动到 GroupBox 控件的位置时,我正在开发一个小型应用程序,我的 xaml 文件中包含滚动查看器的部分如下(我有一个滚动查看器,位于堆栈面板内和 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>
我在stackoverflow中尝试了其他解决方案,但我不知道为什么我不能处理它来工作。
我只有函数的声明
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.