单击按钮时滚动到对象 (C#/Xaml) [英] Scroll to object on Button click (C# /Xaml)

查看:25
本文介绍了单击按钮时滚动到对象 (C#/Xaml)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让 xaml 页面在单击按钮时滚动到特定对象?例如,我的页面顶部有三个按钮.我希望每个按钮都能将滚动查看器移动到页面的顶部、中间和底部.我怎样才能做到这一点?谢谢!

Is there a way to have a xaml page scroll to a particular object on a button click? For example, I have three buttons at the top of my page. I would like each button to move the scrollviewer to the top, middle, and bottom section of the page. How can I make this happen? Thanks!

推荐答案

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

I try to create the Xaml and to solve the problem. This is my solution. This is the Xaml code.

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

}

这篇关于单击按钮时滚动到对象 (C#/Xaml)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆