• 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

  • 相关阅读:
    P2617 Dynamic Rankings[带修主席树(模板)]
    P2617 Dynamic Rankings[带修主席树(模板)]
    P4127 [AHOI2009]同类分布
    P4127 [AHOI2009]同类分布
    P4550 收集邮票 [期望dp]
    P4550 收集邮票 [期望dp]
    P3168 [CQOI2015]任务查询系统
    蓝桥杯 2的次幂表示 递归
    蓝桥杯 最大的算式 dp
    蓝桥杯 未名湖边的烦恼 递推 dp
  • 原文地址:https://www.cnblogs.com/bear831204/p/2526311.html
Copyright © 2020-2023  润新知