当前位置: 首页 > article >正文

WPF制作图片闪烁的自定义控件

1.定义自定义控件

BlinkingImage.cs:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;

namespace YourNamespace
{
    public class BlinkingImage : Control
    {
        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(string), typeof(BlinkingImage), new PropertyMetadata(null, OnImageSourceChanged));

        public static readonly DependencyProperty IsBlinkingProperty =
            DependencyProperty.Register("IsBlinking", typeof(bool), typeof(BlinkingImage), new PropertyMetadata(false, OnIsBlinkingChanged));

        public string ImageSource
        {
            get { return (string)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        public bool IsBlinking
        {
            get { return (bool)GetValue(IsBlinkingProperty); }
            set { SetValue(IsBlinkingProperty, value); }
        }

        static BlinkingImage()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(BlinkingImage), new FrameworkPropertyMetadata(typeof(BlinkingImage)));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            if (IsBlinking)
            {
                StartBlinking();
            }
        }

        private static void OnImageSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as BlinkingImage;
            if (control != null)
            {
                control.UpdateImageSource();
            }
        }

        private static void OnIsBlinkingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as BlinkingImage;
            if (control != null)
            {
                if ((bool)e.NewValue)
                {
                    control.StartBlinking();
                }
                else
                {
                    control.StopBlinking();
                }
            }
        }

        private void UpdateImageSource()
        {
            Image image = GetTemplateChild("PART_Image") as Image;
            if (image != null && !string.IsNullOrEmpty(ImageSource))
            {
                image.Source = new BitmapImage(new Uri(ImageSource, UriKind.RelativeOrAbsolute));
            }
        }

        private void StartBlinking()
        {
            Image image = GetTemplateChild("PART_Image") as Image;
            if (image != null)
            {
                DoubleAnimation animation = new DoubleAnimation
                {
                    From = 1.0,
                    To = 0.0,
                    Duration = new Duration(TimeSpan.FromSeconds(0.5)),
                    AutoReverse = true,
                    RepeatBehavior = RepeatBehavior.Forever
                };
                image.BeginAnimation(OpacityProperty, animation);
            }
        }

        private void StopBlinking()
        {
            Image image = GetTemplateChild("PART_Image") as Image;
            if (image != null)
            {
                image.Opacity = 1.0;
                image.BeginAnimation(OpacityProperty, null);
            }
        }
    }
}

2. 定义控件样式

Generic.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:YourNamespace">
    <Style TargetType="{x:Type local:BlinkingImage}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:BlinkingImage}">
                    <Image x:Name="PART_Image" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

3.将自定义的控件样式添加到App.xaml文件中

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Generic.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

4. 使用自定义控件

MainWindow.xaml:

<Window x:Class="YourNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:YourNamespace"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
           <local:BlinkingImage Grid.Row="1" Grid.Column="0" ImageSource="./image.png"     Width="50" Height="70" IsBlinking="{Binding ElementName=CkOne,Path=IsChecked}"/>
        <CheckBox Grid.Row="1" Grid.Column="1" x:Name="CkOne" IsChecked="False"/>
    </Grid>
</Window>


http://www.kler.cn/a/447454.html

相关文章:

  • 使用 Buildroot 构建带有 Avahi 支持的 Linux 系统
  • UIP协议栈 TCP通信客户端 服务端,UDP单播 广播通信 example
  • 迈向未来:.NET技术的持续创新与发展前景
  • Linux下部署MySQL8.0集群 - 主从复制(一主两从)
  • C 数组:索引魔杖点化的数据星图阵列
  • 04、Vue与Ajax
  • 科研篇——《吕达仁:科研真问题从何而来》
  • volatility2工具的使用vol2工具篇
  • 青少年编程与数学 02-004 Go语言Web编程 09课题、访问数据库
  • define ATL_NO_VTABLE __declspec(novtable)
  • 时间序列预测论文阅读和相关代码库
  • 基于Spring Boot的找律师系统
  • spring事件机制笔记、发布和监听
  • Keil MDK下载程序后MCU自动重启设置
  • ElasticSearch 数据同步
  • 如何重新设置VSCode的密钥环密码?
  • P10425 [蓝桥杯 2024 省 B] R 格式
  • CSGHub开源版本v1.2.0更新
  • 【hackmyvm】p4l4nc4靶场
  • 【前端面试】list转树、拍平, 指标,
  • 堆排序【东北大学oj数据结构9-4】C++
  • ELK系列-(六)Redis也能作为消息队列?(下)
  • 用Python设置Excel工作表的页眉和页脚
  • Python解决安装包报错4.0.0-unsupported
  • 使用支持向量机(SVM)实现二分类
  • 数据倾斜的原因以及解决方法