繁体   English   中英

滚动到按钮单击上的对象(C#/ Xaml)

[英]Scroll to object on Button click (C# /Xaml)

有没有一种方法可以使xaml页面在单击按钮时滚动到特定对象? 例如,我在页面顶部有三个按钮。 我希望每个按钮都将scrollviewer移动到页面的顶部,中间和底部。 我怎样才能做到这一点? 谢谢!

我尝试创建Xaml并解决该问题。 这是我的解决方案。 这是Xaml代码。

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
   <Grid.RowDefinitions>
       <RowDefinition Height="100"></RowDefinition>
       <RowDefinition Height="*"></RowDefinition>
   </Grid.RowDefinitions>

<Button HorizontalAlignment="Left" Content="Top" Click="Button_Click" ></Button>
<Button HorizontalAlignment="Center" Content="Center" Click="Button_Click_1" ></Button><Button Content="Botton" HorizontalAlignment="Right" Click="Button_Click_2" ></Button>
<ScrollViewer Grid.Row="1" Name="MyScrollViewer" MaxZoomFactor="9">
     <StackPanel>
          <TextBlock Name="TextBlock" Height="20"> </TextBlock>
          <Rectangle Height="300" Width="100"  Fill="BlanchedAlmond"></Rectangle>
          <Rectangle Height="300" Width="100"  Fill="Blue"></Rectangle>
          <Rectangle Height="300" Width="100"  Fill="BlueViolet"></Rectangle>
          <Rectangle Height="300" Width="100"  Fill="Chartreuse"></Rectangle>
          <Rectangle Height="300" Width="100"  Fill="Crimson"></Rectangle>
     </StackPanel>
</ScrollViewer>
</Grid>           

这是.cs代码

private void Button_Click(object sender, RoutedEventArgs e)
{
    MyScrollViewer.ScrollToVerticalOffset(0);
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    var scrollableHeight = MyScrollViewer.ScrollableHeight;
    var height= scrollableHeight / 2;
    MyScrollViewer.ScrollToVerticalOffset(height);
}

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    MyScrollViewer.ScrollToVerticalOffset(MaxHeight);
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM