WPF 一个简单的颜色选择器

本章讲述:WPF中,一个简单的颜色选择器的实现;

具体简单的界面显示引用可以参考地址:https://blog.csdn.net/BYH371256/article/details/90520134

使用外部依赖库文件:“ColorPicker.dll”;

下载地址:https://download.csdn.net/download/byh371256/10745273

1、XAML界面代码


    
        
    
    
        
            
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
            
            
                
                
                
            
            
        
    

2、后台逻辑代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ColorInput
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }
        private void Rectangle_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            Brush brush = (sender as Rectangle).Fill;
            Color c = (Color)ColorConverter.ConvertFromString(brush.ToString());
            cpicker.SelectedColor = c;
            if (ColorChangedEvent != null)
                ColorChangedEvent(this, c);
        }

        public event ColorChangedHandler ColorChangedEvent;

        private void cpicker_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            if (this.IsLoaded == false)
                return;
            if (ColorChangedEvent != null)
                ColorChangedEvent(this, e.NewValue);
        }

        public static readonly DependencyProperty ExSelectedColorProperty = DependencyProperty.Register(
                     "ExSelectedColor",
                     typeof(Color),
                     typeof(MainWindow),
                     new PropertyMetadata(Colors.Black));

        public Color ExSelectedColor
        {
            get { return (Color)GetValue(ExSelectedColorProperty); }
            set { SetValue(ExSelectedColorProperty, value); }
        }

        public static readonly DependencyProperty ExSelectedBrushProperty = DependencyProperty.Register(
                      "ExSelectedBrush",
                      typeof(Brush),
                      typeof(MainWindow),
                      new PropertyMetadata(Brushes.Black));

        public Brush ExSelectedBrush
        {
            get { return (Brush)GetValue(ExSelectedBrushProperty); }
            set { SetValue(ExSelectedBrushProperty, value); }
        }
    }

    public delegate void ColorChangedHandler(object sender, Color newColor);
}

3、效果图

WPF 一个简单的颜色选择器_第1张图片

你可能感兴趣的:(WPF,颜色选择器)