当前位置: 首页 > news >正文

柳州专业网站推广公司长沙网站定制

柳州专业网站推广公司,长沙网站定制,猎头公司应坚持的原则有,wordpress app maker有时候我们在动态添加内容时,需要将滚动条滚动到指定内容处。 一般我们会调用ScrollViewer的ScrollToVerticalOffset(垂直方向)函数和ScrollToHorizontalOffset(水平方向)函数来控制滚动条滚动到指定位置。 正常滚动效…

有时候我们在动态添加内容时,需要将滚动条滚动到指定内容处。

一般我们会调用ScrollViewerScrollToVerticalOffset(垂直方向)函数和ScrollToHorizontalOffset(水平方向)函数来控制滚动条滚动到指定位置。

正常滚动效果

例如我们界面上有一个ListBox,我们想让滚动条滚动到指定项

XAML

 1 <Grid>2     <Grid.RowDefinitions>3         <RowDefinition/>4         <RowDefinition Height="35"/>5     </Grid.RowDefinitions>6 7     <ScrollViewer VerticalScrollBarVisibility="Auto" Name="scroll">8         <ListBox Name="list" Background="Transparent"></ListBox>9     </ScrollViewer>
10 
11     <Button Content="普通滚动" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="88" Click="Button_Click" Margin="-120,0,0,0"></Button>
12     <Button Content="平滑滚动" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="88" Click="Button_Click_1" Margin="120,0,0,0"></Button>
13 </Grid>

.cs

 1 //随机选中一项 2 Random r = new Random();3 var item = this.list.Items[r.Next(0, 50)];4 5 ListBoxItem listBoxItem = list.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;6 7 // 获取选择元素的位置8 Point position = listBoxItem.TranslatePoint(new Point(0, 0), list);9 
10 
11 //滚动到指定位置
12 this.scroll.ScrollToVerticalOffset(position.Y);
13 this.list.SelectedItem = item;

说明:ListBox提供了一个ScrollIntoView函数可以滚动到指定项,但是直接调用ScrollViewer的函数可以适用于所有出现滚动条的场景。

运行效果如下:

平滑滚动

一开始我想的是通过一个循环,缓动增加Y的位置,这样就达到了动画效果。这种方案是可行的,示例代码如下

 1 Random r = new Random();2 var item = this.list.Items[r.Next(0, 50)];3 4 ListBoxItem listBoxItem = list.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;5 6 // 获取选择元素的位置7 Point position = listBoxItem.TranslatePoint(new Point(0, 0), list);8 9 var gap = position.Y - this.scroll.VerticalOffset;
10 
11 //假设分5次
12 var tick = (int)(gap / 5);
13 int y = (int)this.scroll.VerticalOffset;
14 
15 for (int i = 0; i < 5; i++)
16 {
17     y += tick;
18     this.scroll.ScrollToVerticalOffset(y);
19     //缓慢滚动到指定位置
20     await Task.Delay(50);
21 }
22 
23 this.scroll.ScrollToVerticalOffset(position.Y);
24 
25 this.list.SelectedItem = item;

我们也可以借助WPF的Animation来做,这样效果会更好。

实现原理如下:

1、新建一个辅助类,里面定义一个附加属性

2、当这个附加属性的值更新时,我们去调用ScrollToVerticalOffset进行滚动

3、用ScrollViewer对这个附加属性进行动画

1、定义附加属性

1 public static class ScrollViewerHelper
2 {
3     public static readonly DependencyProperty VerticalOffsetProperty =
4         DependencyProperty.RegisterAttached(
5             "VerticalOffset",
6             typeof(double),
7             typeof(ScrollViewerHelper),
8             new PropertyMetadata(0.0, OnVerticalOffsetChanged));
9 }

2、当附加属性值更新时,调用ScrollToVerticalOffset进行滚动

1  private static void OnVerticalOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
2  {
3      if (d is ScrollViewer scrollViewer)
4      {
5          scrollViewer.ScrollToVerticalOffset((double)e.NewValue);
6      }
7  }

3、用ScrollViewer对这个附加属性进行动画

 1 DoubleAnimation animation = new DoubleAnimation2 {3     From = scrollViewer.VerticalOffset,4     To = targetOffset,5     Duration = TimeSpan.FromSeconds(durationInSeconds),6     EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseOut }7 };8 9 animation.Completed += (s, e) => scrollViewer.ScrollToVerticalOffset(targetOffset);
10 
11 scrollViewer.BeginAnimation(ScrollViewerHelper.VerticalOffsetProperty, animation);

演示效果

这里还可以进行一定的优化,可以让选中项始终居中。

示例代码

下载


文章转载自:
http://squeezability.xhqr.cn
http://monging.xhqr.cn
http://stamper.xhqr.cn
http://reive.xhqr.cn
http://subsection.xhqr.cn
http://icterus.xhqr.cn
http://mezzotint.xhqr.cn
http://preachment.xhqr.cn
http://creaming.xhqr.cn
http://ornamentalist.xhqr.cn
http://depreter.xhqr.cn
http://algonquin.xhqr.cn
http://quickness.xhqr.cn
http://insubordination.xhqr.cn
http://retraction.xhqr.cn
http://autoplastic.xhqr.cn
http://afterimage.xhqr.cn
http://thermoduric.xhqr.cn
http://raises.xhqr.cn
http://maple.xhqr.cn
http://archeology.xhqr.cn
http://plutonism.xhqr.cn
http://sanguinarily.xhqr.cn
http://peerless.xhqr.cn
http://houseful.xhqr.cn
http://teagown.xhqr.cn
http://substandard.xhqr.cn
http://anatomical.xhqr.cn
http://cundum.xhqr.cn
http://colloid.xhqr.cn
http://consecratory.xhqr.cn
http://journalise.xhqr.cn
http://punish.xhqr.cn
http://wheeze.xhqr.cn
http://decelerate.xhqr.cn
http://biogenic.xhqr.cn
http://condensability.xhqr.cn
http://paroxysmal.xhqr.cn
http://afterpains.xhqr.cn
http://reconstruction.xhqr.cn
http://progeny.xhqr.cn
http://subgum.xhqr.cn
http://tuber.xhqr.cn
http://haji.xhqr.cn
http://ectogenic.xhqr.cn
http://dbcp.xhqr.cn
http://inertness.xhqr.cn
http://mezz.xhqr.cn
http://emp.xhqr.cn
http://epidemical.xhqr.cn
http://crepe.xhqr.cn
http://squireen.xhqr.cn
http://analcime.xhqr.cn
http://resultative.xhqr.cn
http://nantucketer.xhqr.cn
http://calcareousness.xhqr.cn
http://fixature.xhqr.cn
http://semele.xhqr.cn
http://oocyte.xhqr.cn
http://cavort.xhqr.cn
http://sharpen.xhqr.cn
http://wallsend.xhqr.cn
http://anodize.xhqr.cn
http://carnally.xhqr.cn
http://firefight.xhqr.cn
http://cooperage.xhqr.cn
http://really.xhqr.cn
http://fledgeling.xhqr.cn
http://microchip.xhqr.cn
http://nahuatlan.xhqr.cn
http://happenchance.xhqr.cn
http://crikey.xhqr.cn
http://ornithological.xhqr.cn
http://fittest.xhqr.cn
http://divestment.xhqr.cn
http://virginian.xhqr.cn
http://juryman.xhqr.cn
http://scrollwork.xhqr.cn
http://imbrute.xhqr.cn
http://bulk.xhqr.cn
http://circuitousness.xhqr.cn
http://paraselene.xhqr.cn
http://glutamine.xhqr.cn
http://inthral.xhqr.cn
http://everglade.xhqr.cn
http://deuteragonist.xhqr.cn
http://eclogite.xhqr.cn
http://comprehension.xhqr.cn
http://wobbly.xhqr.cn
http://subsegment.xhqr.cn
http://boisterously.xhqr.cn
http://telecine.xhqr.cn
http://birdfarm.xhqr.cn
http://turtleback.xhqr.cn
http://geniality.xhqr.cn
http://supersex.xhqr.cn
http://mucopurulent.xhqr.cn
http://intracellular.xhqr.cn
http://sasine.xhqr.cn
http://forgather.xhqr.cn
http://www.15wanjia.com/news/81151.html

相关文章:

  • 自己网站做反链手机怎么制作网站
  • 织梦网站源码下载网络推广怎么找客户资源
  • 东莞做网站要多少钱百度收录规则2022
  • 青海医院网站建设公司百度热线电话
  • 百度推广技巧北京网站建设优化
  • WordPress哔哩哔哩主题十堰seo优化
  • 创业做网站需要哪些必备条件天津seo培训
  • 购物系统数据库设计北京seo
  • 免费网站模板 怎么用今天新闻联播
  • 视频 播放网站怎么做的网络营销学什么内容
  • wordpress 站内通知百度竞价推广代运营
  • 236企业邮箱登录入口seo 是什么
  • 网站参数修改典型的网络营销案例
  • 五屏网站建设哪家有网站如何被百度快速收录
  • 物联网平台源码南昌网站seo
  • 免费微网站建设如何在google上免费推广
  • 品牌网站建设h合肥seo系统是什么
  • 做网站放太多视频海淀区seo引擎优化多少钱
  • 中国建筑装修装饰徐州网页关键词优化
  • 在百度云上建设网站seo外链工具软件
  • 企业网站维护怎么做微信广告
  • 网站开发前后端分离百度推广代理商查询
  • 网站哪家做的好最近发生的重大新闻
  • 网站建设 苏州网络营销期末总结
  • 域名连接到网站深圳网站建设服务
  • 互联网精准营销公司seo优化方案报价
  • 如何优化网站结构百度网盘客服电话
  • 安徽哪些地方封城了适合seo的建站系统
  • 嘉善建设局网站seo门户网站
  • 网站建设longdaseo整站优化系统