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

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

问题描述

有没有办法有一个XAML页面滚动到一个按钮一个特定的对象单击?例如,我有我的页面顶部的三个按钮。我想每个按钮到的ScrollViewer移动到顶部,中部和页的底部。我怎样才能做到这一点?谢谢!

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天全站免登陆