代码滑动panorama-即程序中设置SelectedIndex
我们都知道panorama的SelectedIndex属性是只读的,所以通过修改它,在程序滑动panorama似乎不可能。那么是不是就没有办法了呢?
其实我们可以通过设置SelectedItemProperty这个依赖属性来改变SelectedItem的值。
设置方法如下:
pan.SetValue(Panorama.SelectedItemProperty, pan.Items[newIndex]);
虽然可以通过程序改变SelectedItem,但是panorama不会立即更新,我们只需要加一个小技巧,代码如下:
(pan.Items[curIndex] as PanoramaItem).Visibility = Visibility.Collapsed;
pan.SetValue(Panorama.SelectedItemProperty, pan.Items[(curIndex + 1) % pan.Items.Count]);
pan.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
(pan.Items[curIndex] as PanoramaItem).Visibility = Visibility.Visible; 这样虽然实现了Item切换,却缺少动画效果,那么我们就需要找到相应的item控件,为其添加RenderTransform动画,即动画的target是RenderTransform。
具体实现如下:
private void slidePanorama(Panorama pan)
{
FrameworkElement panWrapper = VisualTreeHelper.GetChild(pan, 0) as FrameworkElement;
FrameworkElement panTitle = VisualTreeHelper.GetChild(panWrapper, 1) as FrameworkElement;
//Get the panorama layer to calculate all panorama items size
FrameworkElement panLayer = VisualTreeHelper.GetChild(panWrapper, 2) as FrameworkElement;
//Get the title presenter to calculate the title size
FrameworkElement panTitlePresenter = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(panTitle, 0) as FrameworkElement, 1) as FrameworkElement; //Current panorama item index
int curIndex = pan.SelectedIndex; //Get the next of next panorama item
FrameworkElement third = VisualTreeHelper.GetChild(pan.Items[(curIndex + 2) % pan.Items.Count] as PanoramaItem, 0) as FrameworkElement; //Be sure the RenderTransform is TranslateTransform
if (!(pan.RenderTransform is TranslateTransform)
|| !(panTitle.RenderTransform is TranslateTransform))
{
pan.RenderTransform = new TranslateTransform();
panTitle.RenderTransform = new TranslateTransform();
} //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)
pan.Width = 960; //Animate panorama control to the right
Storyboard sb = new Storyboard();
DoubleAnimation a = new DoubleAnimation();
a.From = 0;
a.To = -(pan.Items[curIndex] as PanoramaItem).ActualWidth; //Animate the x transform to a width of one item
a.Duration = new Duration(TimeSpan.FromMilliseconds(700));
a.EasingFunction = new CircleEase(); //This is default panorama easing effect
sb.Children.Add(a);
Storyboard.SetTarget(a, pan.RenderTransform);
Storyboard.SetTargetProperty(a, new PropertyPath(TranslateTransform.XProperty)); //Animate panorama title separately
DoubleAnimation aTitle = new DoubleAnimation();
aTitle.From = 0;
aTitle.To = (panLayer.ActualWidth - panTitlePresenter.ActualWidth) / (pan.Items.Count - 1) * 1.5; //Calculate where should the title animate to
aTitle.Duration = a.Duration;
aTitle.EasingFunction = a.EasingFunction; //This is default panorama easing effect
sb.Children.Add(aTitle);
Storyboard.SetTarget(aTitle, panTitle.RenderTransform);
Storyboard.SetTargetProperty(aTitle, new PropertyPath(TranslateTransform.XProperty)); //Start the effect
sb.Begin(); //After effect completed, we change the selected item
a.Completed += (obj, args) =>
{
//Reset panorama width
pan.Width = 480;
//Change the selected item
(pan.Items[curIndex] as PanoramaItem).Visibility = Visibility.Collapsed;
pan.SetValue(Panorama.SelectedItemProperty, pan.Items[(curIndex + 1) % pan.Items.Count]);
pan.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
(pan.Items[curIndex] as PanoramaItem).Visibility = Visibility.Visible;
//Reset panorama render transform
(pan.RenderTransform as TranslateTransform).X = 0;
//Reset title render transform
(panTitle.RenderTransform as TranslateTransform).X = 0; //Because of the next of next item will be load after we change the selected index to next item
//I do not want it appear immediately without any effect, so I create a custom effect for it
if (!(third.RenderTransform is TranslateTransform))
{
third.RenderTransform = new TranslateTransform();
}
Storyboard sb2 = new Storyboard();
DoubleAnimation aThird = new DoubleAnimation() { From = 100, To = 0, Duration = new Duration(TimeSpan.FromMilliseconds(300)) }; sb2.Children.Add(aThird);
Storyboard.SetTarget(aThird, third.RenderTransform);
Storyboard.SetTargetProperty(aThird, new PropertyPath(TranslateTransform.XProperty));
sb2.Begin();
};
}
代码滑动panorama-即程序中设置SelectedIndex的更多相关文章
- Qt应用程序中设置字体
Qt应用程序中设置字体 应用程序中经常需要设置字体,例如office软件或者是其他的编辑器软件等等.这里主要涉及到如下几个概念:字体,字号以及风格(例如:粗体,斜体,下划线等等).Qt里面也有对应的类 ...
- 中小研发团队架构实践之生产环境诊断工具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如同医生的听诊器,是系统生病时做问题诊断的逆向分析工具 ...
- ZT Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性
Android布局]在程序中设置android:gravity 和 android:layout_Gravity属性 分类: [Android基础] 2011-04-19 16:06 54739人阅读 ...
- 在Winform程序中设置管理员权限及为用户组添加写入权限
在我们一些Winform程序中,往往需要具有一些特殊的权限才能操作系统文件,我们可以设置运行程序具有管理员权限或者设置运行程序的目录具有写入的权限,如果是在操作系统里面,我们可以设置运行程序以管理员身 ...
- (转)在Winform程序中设置管理员权限及为用户组添加写入权限
本文转载自:http://www.cnblogs.com/wuhuacong/p/5645172.html 在我们一些Winform程序中,往往需要具有一些特殊的权限才能操作系统文件,我们可以设置运行 ...
- C#程序中设置全局代理(Global Proxy)
1. HttpWebRequest类的Proxy属性,只要设置了该属性就能够使用代理了,如下: 1 //设置代理 2 WebProxy WP = new Web ...
- [Z] C#程序中设置全局代理(Global Proxy)
https://www.cnblogs.com/Javi/p/7274268.html 1. HttpWebRequest类的Proxy属性,只要设置了该属性就能够使用代理了,如下: 1 ...
- python 程序中设置环境变量
python 中调用系统命令有三种方法: 1.os.system('command') ,这个方法是直接调用标准C的system() 函数,仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 ...
- 【小程序】小程序中设置 tabBar
小程序中 tabBar 的设置,tabBar 就是底部导航栏,在app.json中配置. list 为数组至少两项.tab栏的 position 为 top 时间,不显示图标. "tabBa ...
随机推荐
- VS.net 2013中使用Git建立源代码管理 版本管理
第一次在VS2013中使用Git,也是第一次使用Git,各种不熟悉.百度各种使用经验,大都不屑于使用VS2013集成的Git,建议下载这个下载那个,我也照学了,确实能实现项目的提交.同步.合并的工作, ...
- tar包在linux下 java安装
总的原则:将jdk-7u10-linux-x64.tar.gz压缩包解压至/usr/lib/jdk,设置jdk环境变量并将其修改为系统默认的jdk 1.将jdk-7u5-linux-x64.tar.g ...
- centos安装php php-fpm
centos安装php php-fpm 1.下载php源码包 http://www.php.net/downloads.php 2 .安装php tar -xvf php-5.5.13.tar.bz2 ...
- SQL Server 2012 学习笔记5
1. 索引(Index) 索引是快速的定位和查找数据.索引分为: 聚集索引:唯一,默认主键,一般选取比较连贯的字段,聚集索引是物理排序. 非聚集索引: 并没有把数据物理排序,只是多了一个索引页(包括索 ...
- HBase应用开发回顾与总结系列之二:RowKey行键设计规范
2. RowKey行键设计规范 2.1. RowKey四大特性 2.1.1 字符串类型 虽然行键在HBase中是以byte[]字节数组的形式存储的,但是建议在系统开发过程中将其数据类型设置为Strin ...
- mysql密码相关
Windows中修改密码在不同场景下,有以下几种方案: 方法1:用SET PASSWORD命令,具体更新密码步骤如下: c:>mysql -u root mysql>set passw ...
- ASP.NET Core 1.0 安装并发布到Centos 7.2 使用jexus 5.8.2
安装运行环境 sudoyuminstall libunwind libicu 下载.net core https://www.microsoft.com/net/download 下载完后上传文件 安 ...
- Eclipse 无线调试(利用ADB工具)
首先:1.你的手机必髯要有ROOT权限才可以安装ADB工具:2.手机开启WLAN(WIFI): (1)安装ADB工具: (2)添加环境变量PATHA;(我的是ADB路径是:D:\adt-bundle- ...
- 利用增量备份恢复因归档丢失造成的DG gap
故障现象:data guard归档出现gap,悲剧的是丢失的归档在主库上被rman备份时删除了,丢失的归档大约有20几个,数据库大小约2T,如果重建DG将非常耗时间,因此决定利用增量备份的方式恢复DG ...
- AngularJS几个基础概念
作用域 应用的作用域是和应用的数据模型相关联的,同时作用域也是表达式执行的上下文.$scope对象是定义应用业务逻辑.控制器方法和视图属性的地方.作用域是视图和作用域之间的胶水.在应用将视图渲染并呈现 ...