using System; using System.Windows; using System.Windows.Controls; using System.Windows.Forms; using System.Windows.Media; using XCommon; using XCommon.Log; using Color = System.Windows.Media.Color; using UserControl = System.Windows.Controls.UserControl; namespace XHandler.View.ColorPicker { /// /// 颜色表示按钮 /// public partial class ColorPickerBtn : UserControl { #region 属性 #region RGB字符串值(255,255,0) /// /// RGB /// public static readonly DependencyProperty RGBProperty = DependencyProperty.Register("RGB", typeof(string), typeof(ColorPickerBtn), new FrameworkPropertyMetadata((string)"", OnRGBChanged)); public string RGB { get { return (string)GetValue(RGBProperty); } set { SetValue(RGBProperty, value); } } public static void OnRGBChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ColorPickerBtn colorPickerBtn = (ColorPickerBtn)d; // 颜色设置 colorPickerBtn.SetColor(); } #endregion #endregion #region 事件 /// /// 颜色改变事件 /// public event EventHandler SelectedColorChangedEvent; #endregion #region 构造函数 /// /// 构造函数 /// public ColorPickerBtn() { InitializeComponent(); } #endregion #region 初始化 /// /// 初始化 /// /// /// private void UserControl_Loaded(object sender, RoutedEventArgs e) { try { // 颜色设置 SetColor(); } catch (Exception ex) { LoggerHelper.ErrorLog("ERROR:", ex); } } /// /// 颜色设置 /// public void SetColor() { btnShowColor.Background = new SolidColorBrush(ComUtility.RGBToColor(RGB)); } #endregion /// /// 显示颜色拾取器 /// /// /// private void btnShowColor_Click(object sender, RoutedEventArgs e) { ColorPickerEx colorPickerEx = new ColorPickerEx(); colorPickerEx.ColorBrush = (SolidColorBrush)btnShowColor.Background; colorPickerEx.ShowDialog(); if (colorPickerEx.DialogResult == true) { Color color = colorPickerEx.ColorBrush.Color; //btnShowColor.Background = new SolidColorBrush(color); RGB = $"{color.R},{color.G},{color.B}"; if (SelectedColorChangedEvent != null) { SelectedColorChangedEvent(this, EventArgs.Empty); } } } } }