• XamRibbon中Image可动态旋转的LabelTool


    LabelTool有一个SmallImage属性,是一个ImageSource类型,试图去旋转这个ImageSource没有成功。

    有个类TransformedBitmap可以用来旋转ImageSource,但它只支持90度旋转,所以不能用。

    于是通过更改它的Template实现。

    xaml:

    View Code
    <Window x:Class="RotatingImageInLabelTool.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" xmlns:local="clr-namespace:RotatingImageInLabelTool" xmlns:igRibbon="http://infragistics.com/Ribbon"
            >
        <Grid>
            <igRibbon:XamRibbon Height="147" Margin="8,12,12,0" Name="xamRibbon1" VerticalAlignment="Top">
                <igRibbon:XamRibbon.CommandBindings>
                    <CommandBinding Command="{x:Static local:MainWindow.StartCommand}" Executed="StartCommand_Executed" CanExecute="StartCommand_CanExecute"/>
                    <CommandBinding Command="{x:Static local:MainWindow.StopCommand}" Executed="StopCommand_Executed" CanExecute="StopCommand_CanExecute"/>
                </igRibbon:XamRibbon.CommandBindings>
                <igRibbon:RibbonTabItem Header="Rotating Image">
                    <igRibbon:RibbonGroup Width="250">
                        <igRibbon:LabelTool x:Name="lt" Caption="This is a LabelTool with rotatable image">
                            <igRibbon:LabelTool.Template>
                                <ControlTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <local:RotatingImage x:Name="PART_IMAGE" Source="Icon.png" Width="16" Height="16">
                                            <local:RotatingImage.RenderTransform>
                                                <RotateTransform Angle="0" CenterX="8" CenterY="8"></RotateTransform>
                                            </local:RotatingImage.RenderTransform>
                                            <local:RotatingImage.Style>
                                                <Style>
                                                    <Style.Triggers>
                                                        <EventTrigger RoutedEvent="local:RotatingImage.StartRotating">
                                                            <EventTrigger.Actions>
                                                                <BeginStoryboard x:Name="rotatingStoryboard">
                                                                    <Storyboard>
                                                                        <DoubleAnimation Storyboard.TargetProperty="(local:RotatingImage.RenderTransform).(RotateTransform.Angle)" From="0" To="360" Duration="0:0:0.5" RepeatBehavior="Forever"/>
                                                                    </Storyboard>
                                                                </BeginStoryboard>
                                                            </EventTrigger.Actions>
                                                        </EventTrigger>
                                                        <EventTrigger RoutedEvent="local:RotatingImage.StopRotating">
                                                            <EventTrigger.Actions>
                                                                <StopStoryboard BeginStoryboardName="rotatingStoryboard"/>
                                                            </EventTrigger.Actions>
                                                        </EventTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </local:RotatingImage.Style>
                                        </local:RotatingImage>
                                        <Label Content="{TemplateBinding igRibbon:LabelTool.Caption}"
                                               Foreground="{TemplateBinding igRibbon:LabelTool.Foreground}"
                                               />
                                    </StackPanel>
                                </ControlTemplate>
                            </igRibbon:LabelTool.Template>
                        </igRibbon:LabelTool>
                    </igRibbon:RibbonGroup>
                    <igRibbon:RibbonGroup>
                        <igRibbon:ButtonTool Command="{x:Static local:MainWindow.StartCommand}">Start rotating</igRibbon:ButtonTool>
                        <igRibbon:ButtonTool Command="{x:Static local:MainWindow.StopCommand}">Stop rotating</igRibbon:ButtonTool>
                    </igRibbon:RibbonGroup>
                </igRibbon:RibbonTabItem>
            </igRibbon:XamRibbon>
        </Grid>
    </Window>

    code:

    View Code
    namespace RotatingImageInLabelTool
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            private bool _isRotating;
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            public static readonly RoutedCommand StartCommand = new RoutedCommand();
            public static readonly RoutedCommand StopCommand = new RoutedCommand();
    
            private void StartCommand_Executed(object sender, ExecutedRoutedEventArgs e)
            {
                var image = lt.Template.FindName("PART_IMAGE", lt) as RotatingImage;
                if (image != null)
                    image.RaiseEvent(new RoutedEventArgs(RotatingImage.StartRotatingEvent));
                _isRotating = true;
            }
    
            private void StartCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
            {
                e.CanExecute = !_isRotating;
            }
    
            private void StopCommand_Executed(object sender, ExecutedRoutedEventArgs e)
            {
                var image = lt.Template.FindName("PART_IMAGE", lt) as RotatingImage;
                if (image != null)
                    image.RaiseEvent(new RoutedEventArgs(RotatingImage.StopRotatingEvent));
                _isRotating = false;
            }
    
            private void StopCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
            {
                e.CanExecute = _isRotating;
            }
        }
    
        public class RotatingImage : Image
        {
            public static readonly RoutedEvent StartRotatingEvent = EventManager.RegisterRoutedEvent("StartRotating", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(RotatingImage));
            public event RoutedEventHandler StartRotating
            {
                add { this.AddHandler(StartRotatingEvent, value); }
                remove { this.RemoveHandler(StartRotatingEvent, value); }
            }
    
            public static readonly RoutedEvent StopRotatingEvent = EventManager.RegisterRoutedEvent("StopRotating", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(RotatingImage));
            public event RoutedEventHandler StopRotating
            {
                add { this.AddHandler(StopRotatingEvent, value); }
                remove { this.RemoveHandler(StopRotatingEvent, value); }
            }
        }
    }

    示例:https://files.cnblogs.com/bear831204/RotatingImageInLabelTool.zip

    参考文章:http://www.galasoft.ch/mydotnet/articles/article-2006102701.aspx

  • 相关阅读:
    Win10版《芒果TV》全平台直播第89届奥斯卡颁奖典礼,特设第二演播室带来一手资讯
    Win10《芒果TV》商店版跻身Windows商店《热门免费应用》前12强
    Win10《芒果TV》商店版更新v3.2.6:修复后台任务故障,优化推送频次
    Win10《芒果TV》商店版更新v3.2.5:新增会员频道,修复多处细节问题,小年快乐
    Win10《芒果TV》商店版2016-2017春节大礼,每日前100名用户免费领取7天VIP
    Win10版《芒果TV》获评2016年度Windows Store最佳官方/休闲娱乐应用(LiveSino和微软信仰中心联合评选)
    [Cocos2d-x for WP8学习笔记] 一些基本概念,建立自己的启动界面
    [Cocos2d-x for WP8学习笔记] HelloWorld结构分析
    [Cocos2d-x for WP8学习笔记] HelloWorld
    WindowsPhone使用HtmlAgilityPack解析HTML
  • 原文地址:https://www.cnblogs.com/bear831204/p/2526311.html
Copyright © 2020-2023  润新知