WPF 一个简单的颜色选择器
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BYH371256/article/details/83409995
本章讲述:WPF中,一个简单的颜色选择器的实现;
使用外部依赖库文件:“ColorPicker.dll”;
下载地址:https://download.csdn.net/download/byh371256/10745273
1、XAML界面代码
<Window x:Class="ColorInput.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ColorPicker;assembly=ColorPicker"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="cprct" TargetType="{x:Type Rectangle}">
<Setter Property="Width" Value="25"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Margin" Value="1"/>
<Setter Property="Stroke" Value="Gray"/>
<Setter Property="StrokeThickness" Value="1"/>
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="Rectangle_PreviewMouseLeftButtonDown"/>
</Style>
</Window.Resources>
<Border VerticalAlignment="Center" Width="280" Background="#f0f0f0">
<StackPanel>
<WrapPanel HorizontalAlignment="Center">
<Rectangle Style="{StaticResource cprct}" Fill="White"/>
<Rectangle Style="{StaticResource cprct}" Fill="Gray"/>
<Rectangle Style="{StaticResource cprct}" Fill="Yellow"/>
<Rectangle Style="{StaticResource cprct}" Fill="Orange"/>
<Rectangle Style="{StaticResource cprct}" Fill="HotPink"/>
<Rectangle Style="{StaticResource cprct}" Fill="Peru"/>
<Rectangle Style="{StaticResource cprct}" Fill="RoyalBlue"/>
<Rectangle Style="{StaticResource cprct}" Fill="SkyBlue"/>
<Rectangle Style="{StaticResource cprct}" Fill="Teal"/>
<Rectangle Style="{StaticResource cprct}" Fill="Tomato"/>
<Rectangle Style="{StaticResource cprct}" Fill="Black"/>
<Rectangle Style="{StaticResource cprct}" Fill="Sienna"/>
<Rectangle Style="{StaticResource cprct}" Fill="Gold"/>
<Rectangle Style="{StaticResource cprct}" Fill="DarkBlue"/>
<Rectangle Style="{StaticResource cprct}" Fill="Magenta"/>
<Rectangle Style="{StaticResource cprct}" Fill="LimeGreen"/>
<Rectangle Style="{StaticResource cprct}" Fill="Lime"/>
<Rectangle Style="{StaticResource cprct}" Fill="Blue"/>
<Rectangle Style="{StaticResource cprct}" Fill="Green"/>
<Rectangle Style="{StaticResource cprct}" Fill="Red"/>
</WrapPanel>
<StackPanel Orientation="Horizontal" Height="30">
<TextBlock Text="ARGB:" VerticalAlignment="Center" Margin="16,0,10,0"/>
<TextBox Text="{Binding ElementName=cpicker, Path=SelectedColor, Mode=TwoWay}" VerticalAlignment="Center" Width="90"/>
<Rectangle Stroke="Gray" StrokeThickness="1" Width="80" Height="22" Margin="10,4,4,4"
Fill="{Binding ElementName=cpicker, Path=SelectedBrush}"/>
</StackPanel>
<local:ColorPicker x:Name="cpicker" SelectedColorChanged="cpicker_SelectedColorChanged"
SelectedColor="{Binding ElementName=clrctrl, Path= ExSelectedColor,Mode=OneWayToSource}"
SelectedBrush="{Binding ElementName= clrctrl,Path=ExSelectedBrush, Mode=TwoWay}" />
</StackPanel>
</Border>
</Window>
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
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
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<Color> 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 一个简单的颜色选择器的更多相关文章
- 从头学Qt Quick(3)-- 用QML写一个简单的颜色选择器
先看一下效果图: 实现功能:点击不同的色块可以改变文字的颜色. 实现步骤: 一.创建一个默认的Qt Quick工程: 二.添加文件Cell.qml 这一步主要是为了实现一个自定义的组件,这个组件就是我 ...
- WPF一个简单的垂直菜单样式的实现
以前制作类似于垂直菜单功能的控件我都是Listbox和一个Popup实现的,今天尝试着用Menu做了一个简单垂直菜单,就当是做了个小练习写了这篇随笔~: 有什么不对的地方希望大家指正,分享和记录也是一 ...
- 自定义实现简单的Android颜色选择器(附带源码)
在写Android App过程中需要一个简单的颜色选择器,Android自带的ColorPicker和网上的一些ColorPicker都太高端了,都实现了颜色渐变功能,我要的不需要那么复杂,只想提供几 ...
- 11个JavaScript颜色选择器插件
几年前,很难找到一个合适的颜色选择器.正好看到很多不错的JavaScript颜色选择器插件,故而把这些编译汇总.在本文,Web设计师和开发人员 Kevin Liew 选取了11个相应插件,有些会比较复 ...
- WPF 自己做一个颜色选择器
程序开发过程中,经常会遇到需要支持动态配置主题颜色的问题,通常,一个程序会有多种不同的颜色风格主题供选 有时候,更细致一些的地方,会需要支持自己配置颜色,这样我们就需要一个颜色选择器啦,下面是我自己开 ...
- Prism for WPF 搭建一个简单的模块化开发框架(五)添加聊天、消息模块
原文:Prism for WPF 搭建一个简单的模块化开发框架(五)添加聊天.消息模块 中秋节假期没事继续搞了搞 做了各聊天的模块,需要继续优化 第一步画页面 页面参考https://github.c ...
- 【canvas系列】用canvas实现一个colorpicker(类似PS的颜色选择器)
每个浏览器都有自己的特点,比如今天要做的colorpicker就是,一千个浏览器,一千个哈姆雷特,一千个colorpicker.今天canvas系列就用canvas做一个colorpicker. ** ...
- WInform 创建一个简单的WPF应用
(一)创建一个简单的WPF应用 首先,在这里我要说明的是:这里的例子,都是通过控制台程序来创建WPF应用,而非使用现成的WPF模版.因为WPF模版封装了创建WPF应用所需要的各种基本元素,并不利于我们 ...
- 【WPF】学习笔记(一)——做一个简单的电子签名板
参加实习(WPF)已经有两个多周的时间了,踩了一些坑,也算积累了一些小东西,准备慢慢拿出来分享一下.(●'◡'●) 这次呢就讲讲一个简单的电子签名板的实现. 先上张图(PS:字写得比较丑,不要太在意哈 ...
随机推荐
- ApplicationEventPublisher笔记
ApplicationEventPublisher是ApplicationContext的父接口之一.这接口的作用是:Interface that encapsulates event publica ...
- asp,mdb,工具
<%set conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLE ...
- [BJWC2018]最长上升子序列
十分感谢GXZ大佬的讲解,此处致以敬意!emmmm在初学状压DP时就理解了如此精妙的一道题,感到很开森~ \(Address\) ________________ #\(\color{red}{\ma ...
- 3.高并发教程-基础篇-之分布式全文搜索引擎elasticsearch的搭建
高并发教程-基础篇-之分布式全文搜索引擎elasticsearch的搭建 如果大家看了我的上一篇<2.高并发教程-基础篇-之nginx+mysql实现负载均衡和读写分离>文章,如果能很好的 ...
- Java中的集合和常用类
Java中的常用类: ▪ Object类 ▪ Math类 ▪ String类和StringBuffer类(字符串) ▪ 8种基本类型所对应的包装类 ▪ java.util包中的类——Date类 Obj ...
- 关于UIScrollView无限循环滑动
在使用某宝或某东购物的时候,我们会在其首页看到一个可以滑动的版块,这个版块的实现就是一个UIScrollView.在我们使用UIScrollView的时候会发现,滑动到最后的时候,UIScrollVi ...
- react脚手架环境搭建流程
1.安装与配置node.js:1.1软件下载地址:https://nodejs.org/en/,推荐下载.msi文件,其中npm已经集成在了node.js中.1.2 双击下载的.msi文件进行安装,安 ...
- 大数据时代数据库-云HBase架构&生态&实践
业务的挑战 存储量量/并发计算增大 现如今大量的中小型公司并没有大规模的数据,如果一家公司的数据量超过100T,且能通过数据产生新的价值,基本可以说是大数据公司了 .起初,一个创业公司的基本思路就是首 ...
- 面试被问到IIC,总结。
Linux3.5内核中,IIC. i2c_add_driver i2c_register_driver a. at24cxx_driver放入i2c_bus_type的drv链表 并且从dev链表里取 ...
- 【Keil】Keil5-改变字的大小和颜色
点击 Edit / Configuration 出现弹窗,选择Colors & Fonts 底下可以选择要改哪个部分,例如普通代码.数字.关键字.注释...等等,当然也可以改变背景颜色的,英文 ...