我们都知道panorama的SelectedIndex属性是只读的,所以通过修改它,在程序滑动panorama似乎不可能。那么是不是就没有办法了呢?
其实我们可以通过设置SelectedItemProperty这个依赖属性来改变SelectedItem的值。
设置方法如下:

pan.SetValue(Panorama.SelectedItemProperty, pan.Items[newIndex]);

虽然可以通过程序改变SelectedItem,但是panorama不会立即更新,我们只需要加一个小技巧,代码如下:

  1. (pan.Items[curIndex] as PanoramaItem).Visibility = Visibility.Collapsed;
  2. pan.SetValue(Panorama.SelectedItemProperty, pan.Items[(curIndex + 1) % pan.Items.Count]);
  3. pan.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  4. (pan.Items[curIndex] as PanoramaItem).Visibility = Visibility.Visible;
  5.  
  6. 这样虽然实现了Item切换,却缺少动画效果,那么我们就需要找到相应的item控件,为其添加RenderTransform动画,即动画的targetRenderTransform
    具体实现如下:
  1. private void slidePanorama(Panorama pan)
  2. {
  3. FrameworkElement panWrapper = VisualTreeHelper.GetChild(pan, 0) as FrameworkElement;
  4. FrameworkElement panTitle = VisualTreeHelper.GetChild(panWrapper, 1) as FrameworkElement;
  5. //Get the panorama layer to calculate all panorama items size
  6. FrameworkElement panLayer = VisualTreeHelper.GetChild(panWrapper, 2) as FrameworkElement;
  7. //Get the title presenter to calculate the title size
  8. FrameworkElement panTitlePresenter = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(panTitle, 0) as FrameworkElement, 1) as FrameworkElement;
  9.  
  10. //Current panorama item index
  11. int curIndex = pan.SelectedIndex;
  12.  
  13. //Get the next of next panorama item
  14. FrameworkElement third = VisualTreeHelper.GetChild(pan.Items[(curIndex + 2) % pan.Items.Count] as PanoramaItem, 0) as FrameworkElement;
  15.  
  16. //Be sure the RenderTransform is TranslateTransform
  17. if (!(pan.RenderTransform is TranslateTransform)
  18. || !(panTitle.RenderTransform is TranslateTransform))
  19. {
  20. pan.RenderTransform = new TranslateTransform();
  21. panTitle.RenderTransform = new TranslateTransform();
  22. }
  23.  
  24. //Increase width of panorama to let it render the next slide (if not, default panorama is 480px and the null area appear if we transform it)
  25. pan.Width = 960;
  26.  
  27. //Animate panorama control to the right
  28. Storyboard sb = new Storyboard();
  29. DoubleAnimation a = new DoubleAnimation();
  30. a.From = 0;
  31. a.To = -(pan.Items[curIndex] as PanoramaItem).ActualWidth; //Animate the x transform to a width of one item
  32. a.Duration = new Duration(TimeSpan.FromMilliseconds(700));
  33. a.EasingFunction = new CircleEase(); //This is default panorama easing effect
  34. sb.Children.Add(a);
  35. Storyboard.SetTarget(a, pan.RenderTransform);
  36. Storyboard.SetTargetProperty(a, new PropertyPath(TranslateTransform.XProperty));
  37.  
  38. //Animate panorama title separately
  39. DoubleAnimation aTitle = new DoubleAnimation();
  40. aTitle.From = 0;
  41. aTitle.To = (panLayer.ActualWidth - panTitlePresenter.ActualWidth) / (pan.Items.Count - 1) * 1.5; //Calculate where should the title animate to
  42. aTitle.Duration = a.Duration;
  43. aTitle.EasingFunction = a.EasingFunction; //This is default panorama easing effect
  44. sb.Children.Add(aTitle);
  45. Storyboard.SetTarget(aTitle, panTitle.RenderTransform);
  46. Storyboard.SetTargetProperty(aTitle, new PropertyPath(TranslateTransform.XProperty));
  47.  
  48. //Start the effect
  49. sb.Begin();
  50.  
  51. //After effect completed, we change the selected item
  52. a.Completed += (obj, args) =>
  53. {
  54. //Reset panorama width
  55. pan.Width = 480;
  56. //Change the selected item
  57. (pan.Items[curIndex] as PanoramaItem).Visibility = Visibility.Collapsed;
  58. pan.SetValue(Panorama.SelectedItemProperty, pan.Items[(curIndex + 1) % pan.Items.Count]);
  59. pan.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  60. (pan.Items[curIndex] as PanoramaItem).Visibility = Visibility.Visible;
  61. //Reset panorama render transform
  62. (pan.RenderTransform as TranslateTransform).X = 0;
  63. //Reset title render transform
  64. (panTitle.RenderTransform as TranslateTransform).X = 0;
  65.  
  66. //Because of the next of next item will be load after we change the selected index to next item
  67. //I do not want it appear immediately without any effect, so I create a custom effect for it
  68. if (!(third.RenderTransform is TranslateTransform))
  69. {
  70. third.RenderTransform = new TranslateTransform();
  71. }
  72. Storyboard sb2 = new Storyboard();
  73. DoubleAnimation aThird = new DoubleAnimation() { From = 100, To = 0, Duration = new Duration(TimeSpan.FromMilliseconds(300)) };
  74.  
  75. sb2.Children.Add(aThird);
  76. Storyboard.SetTarget(aThird, third.RenderTransform);
  77. Storyboard.SetTargetProperty(aThird, new PropertyPath(TranslateTransform.XProperty));
  78. sb2.Begin();
  79. };
  80. }

代码滑动panorama-即程序中设置SelectedIndex的更多相关文章

  1. Qt应用程序中设置字体

    Qt应用程序中设置字体 应用程序中经常需要设置字体,例如office软件或者是其他的编辑器软件等等.这里主要涉及到如下几个概念:字体,字号以及风格(例如:粗体,斜体,下划线等等).Qt里面也有对应的类 ...

  2. 中小研发团队架构实践之生产环境诊断工具WinDbg 三分钟学会.NET微服务之Polly 使用.Net Core+IView+Vue集成上传图片功能 Fiddler原理~知多少? ABP框架(asp.net core 2.X+Vue)模板项目学习之路(一) C#程序中设置全局代理(Global Proxy) WCF 4.0 使用说明 如何在IIS上发布,并能正常访问

    中小研发团队架构实践之生产环境诊断工具WinDbg 生产环境偶尔会出现一些异常问题,WinDbg或GDB是解决此类问题的利器.调试工具WinDbg如同医生的听诊器,是系统生病时做问题诊断的逆向分析工具 ...

  3. ZT Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性

    Android布局]在程序中设置android:gravity 和 android:layout_Gravity属性 分类: [Android基础] 2011-04-19 16:06 54739人阅读 ...

  4. 在Winform程序中设置管理员权限及为用户组添加写入权限

    在我们一些Winform程序中,往往需要具有一些特殊的权限才能操作系统文件,我们可以设置运行程序具有管理员权限或者设置运行程序的目录具有写入的权限,如果是在操作系统里面,我们可以设置运行程序以管理员身 ...

  5. (转)在Winform程序中设置管理员权限及为用户组添加写入权限

    本文转载自:http://www.cnblogs.com/wuhuacong/p/5645172.html 在我们一些Winform程序中,往往需要具有一些特殊的权限才能操作系统文件,我们可以设置运行 ...

  6. C#程序中设置全局代理(Global Proxy)

    1. HttpWebRequest类的Proxy属性,只要设置了该属性就能够使用代理了,如下: 1             //设置代理 2         WebProxy WP = new Web ...

  7. [Z] C#程序中设置全局代理(Global Proxy)

    https://www.cnblogs.com/Javi/p/7274268.html 1. HttpWebRequest类的Proxy属性,只要设置了该属性就能够使用代理了,如下: 1        ...

  8. python 程序中设置环境变量

    python 中调用系统命令有三种方法: 1.os.system('command') ,这个方法是直接调用标准C的system() 函数,仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 ...

  9. 【小程序】小程序中设置 tabBar

    小程序中 tabBar 的设置,tabBar 就是底部导航栏,在app.json中配置. list 为数组至少两项.tab栏的 position 为 top 时间,不显示图标. "tabBa ...

随机推荐

  1. WPF学习之路(十二)控件(HeaderedContent控件)

    GroupBox 用来组织多种控件的常见控件,因为是内容空间,只能直接包含一项,需要使用面板一类的中间空间. Header和Content可以是任意元素 <GroupBox> <Gr ...

  2. 哭瞎!360云盘将关停,你的几十T照片和文件该怎么办

    IDO老徐刚得到了一个非常不开心的消息,360云盘将停止个人云盘服务...进行业务转型,在网盘存储.传播内容的合法性和安全性得到彻底解决之前不再考虑恢复,之后转型企业云服务. 而且之前共享的所有资料, ...

  3. 集合1--毕向东java基础教程视频学习笔记

    Day14 集合框架01 体系概述02 共性方法03 迭代器04 List集合共性方法05 ListIterator06 List集合具体对象特点07 Vector中的枚举 01 体系概述 集合类为什 ...

  4. Tomcat:基于HTTP协议的Connector配置

    Tomcat Connector 是请求接收环节与请求处理环节的连接器,具体点说,就是将接收到的请求传递给Tomcat WEB容器进行处理. Tomcat可以处理的不同协议的请求,例如HTTP协议.A ...

  5. SQLHelp帮助类

    public readonly static string connStr = ConfigurationManager.ConnectionStrings["conn"].Con ...

  6. Java调优

    Java调优经验谈 对于调优这个事情来说,一般就是三个过程: 性能监控:问题没有发生,你并不知道你需要调优什么?此时需要一些系统.应用的监控工具来发现问题. 性能分析:问题已经发生,但是你并不知道问题 ...

  7. ES6函数默认参数(Default Parameters)

    语言更新时每一个新增的特性都是从千百万开发者需求里提取过来的,规范采用后能减少程序员的痛苦,带来便捷. 我们经常会这么写 function calc(x, y) { x = x || 0; y = y ...

  8. ajax小结

    1. http是一种无状态协议 2. http请求:四部分组成 ① http 请求的方法或动作,如:GET / POST ② 正在请求的URL,总得知道请求的地址是什么 ③ 请求头,包含一些客户端环境 ...

  9. Struts2中的Unable to load configuration错误的分析与解决方法

    当我们遇到 Unable to load configuration. 这样的错误时,可以根据具体的错误提示找出错误的原因. Unable to load configuration. - inter ...

  10. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...