开发者社区> 杨俊明> 正文

Silverlight Telerik控件学习:RadTransitionControl

简介: 如果展示类似这种比较cool的图片轮换效果,用RadTransitionControl控件就对了,它提供的过渡效果非常cool! 原理并不复杂,可参见以前写的 Silverlight之ListBox/Style学习笔记--ListBox版的图片轮换广告.
+关注继续查看

img_21ccebe44fc46e65939e6564d1ee6412.png

如果展示类似这种比较cool的图片轮换效果,用RadTransitionControl控件就对了,它提供的过渡效果非常cool!

原理并不复杂,可参见以前写的 Silverlight之ListBox/Style学习笔记--ListBox版的图片轮换广告.

xaml部分:

<UserControl xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"  x:Class="Telerik.Sample.Transition"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:common="clr-namespace:Common.Silverlight;assembly=Common.Silverlight"
    xmlns:model="clr-namespace:BusinessObject;assembly=BusinessObject"
    xmlns:telerikTranstions="clr-namespace:Telerik.Windows.Controls.TransitionEffects;assembly=Telerik.Windows.Controls"
    mc:Ignorable="d"
    d:DesignHeight="313" d:DesignWidth="500" >
    <UserControl.Resources>

        <!--列表框的样式-->
        <Style x:Key="PhotoListStyle" TargetType="ListBox">
            <Setter Property="Background" Value="{x:Null}"/>
            <Setter Property="BorderThickness" Value="0"/>
        </Style>

        <!--列表框-数据项的样式-->
        <Style x:Key="PhotoListItemStyle" TargetType="ListBoxItem">
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Margin" Value="1"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Background" Value="{x:Null}"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="TabNavigation" Value="Local"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Grid  Cursor="Hand">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To=".35" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="RectMouseOver"/>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="contentPresenter"/>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="SelectionStates">
                                    <VisualState x:Name="Unselected"/>
                                    <VisualState x:Name="Selected">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To=".75" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="RectSelected"/>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="FocusStates">
                                    <VisualState x:Name="Focused">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="FocusVisualElement">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Visible</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Unfocused"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Rectangle x:Name="RectMouseOver" Fill="AliceBlue" IsHitTestVisible="False" Opacity="0"  StrokeThickness="0" Margin="1"/>
                            <Rectangle x:Name="RectSelected" Fill="AntiqueWhite" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1" StrokeThickness="0" Margin="1"/>
                            <ContentPresenter x:Name="contentPresenter" 
								ContentTemplate="{TemplateBinding ContentTemplate}" 
								Content="{TemplateBinding Content}" 
								HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
								Margin="{TemplateBinding Padding}"/>
                            <Rectangle x:Name="FocusVisualElement" RadiusY="1" RadiusX="1" Stroke="#FFEFEFEF" StrokeThickness="0.5" Visibility="Visible" d:IsHidden="True"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!--列表框-布局模板-->
        <ItemsPanelTemplate x:Key="HorizontalItemPanel">
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>

        <!--列表框-数据项(内容)模板-->
        <DataTemplate x:Key="DataTemplate">
            <Grid Width="22" Height="22" Background="#11FFFFFF">
                <TextBlock Text="{Binding Text}" ToolTipService.ToolTip="{Binding Description}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontSize="10" FontWeight="Bold"></TextBlock>
            </Grid>
        </DataTemplate>

        <!--示例数据源-->
        <common:ObjectCollection x:Key="PhotosCollection">
            <model:MenuItem ImageUri="/Common.Silverlight.Resource;component/img/ireland01.jpg" Text="1" Description="爱尔兰风景1"></model:MenuItem>
            <model:MenuItem ImageUri="/Common.Silverlight.Resource;component/img/ireland02.jpg" Text="2" Description="爱尔兰风景2"></model:MenuItem>
            <model:MenuItem ImageUri="/Common.Silverlight.Resource;component/img/ireland03.jpg" Text="3" Description="爱尔兰风景3"></model:MenuItem>
            <model:MenuItem ImageUri="/Common.Silverlight.Resource;component/img/ireland04.jpg" Text="4" Description="爱尔兰风景4"></model:MenuItem>
            <model:MenuItem ImageUri="/Common.Silverlight.Resource;component/img/ireland05.jpg" Text="5" Description="爱尔兰风景5"></model:MenuItem>
        </common:ObjectCollection>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot">
        <Grid VerticalAlignment="Center" HorizontalAlignment="Center"  Width="500" Height="313" MouseEnter="Grid_MouseEnter" MouseLeave="Grid_MouseLeave">
            
            <telerik:RadTransitionControl  Content="{Binding SelectedItem, ElementName=PhotosListBox}" >
                <telerik:RadTransitionControl.Transition>
                    <telerikTranstions:MotionBlurredZoomTransition />
                </telerik:RadTransitionControl.Transition>
                <telerik:RadTransitionControl.ContentTemplate>
                    <DataTemplate>
                        <Image Source="{Binding ImageUri}" Stretch="Uniform" />
                    </DataTemplate>
                </telerik:RadTransitionControl.ContentTemplate>
            </telerik:RadTransitionControl>

            <ListBox x:Name="PhotosListBox" 
				ItemsSource="{StaticResource PhotosCollection}" 
				ItemsPanel="{StaticResource HorizontalItemPanel}" 
				ItemTemplate="{StaticResource DataTemplate}"  
				VerticalAlignment="Bottom"  
				HorizontalAlignment="Right" 
				Margin="5" 
                SelectedIndex="0" 
				ItemContainerStyle="{StaticResource PhotoListItemStyle}" 
				Style="{StaticResource PhotoListStyle}"/>
        </Grid>
        
    </Grid>
</UserControl>

后端cs代码

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace Telerik.Sample
{
    public partial class Transition : UserControl
    {
       
        private DispatcherTimer timer = new DispatcherTimer();
        private bool isForward = true;

        public Transition()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(Transition_Loaded);
        }

        void Transition_Loaded(object sender, RoutedEventArgs e)
        {            
            timer.Interval = new TimeSpan(0, 0, 0, 0, 2000);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            var _currentIndex = this.PhotosListBox.SelectedIndex;
            if (isForward) //如果 从左向右 递增 翻
            {
                if (_currentIndex < this.PhotosListBox.Items.Count - 1)
                {
                    this.PhotosListBox.SelectedIndex++;                    
                }
                else
                {
                    this.PhotosListBox.SelectedIndex--;                    
                    isForward = false;//换方向
                }
            }
            else 
            {
                if (_currentIndex > 0)
                {
                    this.PhotosListBox.SelectedIndex--;                    
                }
                else 
                {
                    this.PhotosListBox.SelectedIndex++;                   
                    isForward = true;
                }
            }            
        }

        private void Grid_MouseEnter(object sender, MouseEventArgs e)
        {
            timer.Stop();
        }

        private void Grid_MouseLeave(object sender, MouseEventArgs e)
        {
            timer.Start();
        }
    }
}

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Silverlight RadTreeView 控件使用介绍
1、RadTreeView是Telerik公司提供的控件,与之对应的也有SL平台下的控件,使用方法跟Silverlight ToolKit中的TreeView中使用的HierarchicalDataTemplate类似,可以实现多级递归。
669 0
Telerik Silverlight 之Charting控件的使用
效果还是不错的,还能设置动态加载效果,如下图:                                                                        ...
738 0
Silverlight Telerik控件学习:弹出窗口RadWindow
几乎所有的业务系统都有弹出窗口,典型场景有二种 : 1、简单的弹出一个对话框显示信息,比如下面这样: 这个很简单,代码示例如下: DialogParameters pars = new DialogParameters(); pars.
1050 0
Silverlight中实现类似Telerik的TileView控件效果
暂时没时间写,等有时间在写下。
518 0
Silverlight控件 - Carrousel
Silverlight控件 - Carrousel nasa.wang 2009.03.05 简介: Carrousel是一个布局控件,可对其内部的子控件排出像《旋转木马》一样的效果。
699 0
Silverlight控件 - ScatterView
Silverlight控件 - ScatterView nasa.wang 2009.03.03 简介: ScatterView是一个布局控件,允许对其内部的子控件进行鼠标拖拽、扔出等操作。
742 0
Silverlight 2 中简单的2.5D控件
“Expression Blend and Design products ” 团队发布了一款简单的2.5D控件,目前没有照明效果,材质,线条和多边形。只是最简单的圆形形状。能通过鼠标的拖拽来转动摄像机的位置,按住Ctrl键拖拽鼠标能放大。
678 0
丰富的silverlight控件
demo地址:http://demo.componentone.com/Silverlight/ControlExplorer/ 另外一个 微软示例站点.http://www.codeplex.com/mscuihttp://www.
665 0
+关注
杨俊明
菩提树下的杨过 http://yjmyzz.cnblogs.com/
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载