You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
5.1 KiB

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using HandyControl.Controls;
using HandyControl.Tools;
namespace SparkClient.Views.Configuration.SettingPages;
public partial class ModelColorSetPage
{
public ModelColorSetPage()
{
InitializeComponent();
}
private void LabelsColor_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sender is Label label)
{
var picker = SingleOpenHelper.CreateControl<ColorPicker>();
var window = new PopupWindow
{
PopupElement = picker
};
picker.SelectedBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString((label.Content??"#FFFFFF").ToString()));
picker.Canceled += delegate { window.Close(); };
picker.Confirmed += delegate
{
label.Background = picker.SelectedBrush;
label.Foreground = picker.SelectedBrush;
label.Content = picker.SelectedBrush.ToString();
window.Close();
};
window.Show(label, false);
}
}
public SolidColorBrush GetInverseBrush(SolidColorBrush selectedBrush)
{
Color originalColor = selectedBrush.Color;
Color inverseColor = Color.FromRgb((byte)(255 - originalColor.R),
(byte)(255 - originalColor.G),
(byte)(255 - originalColor.B));
return new SolidColorBrush(inverseColor);
}
private void Label_ContentChanged(object sender, RoutedEventArgs e)
{
if (e.Source is Label label)
{
var newContent = label.Content?.ToString();
// 在这里处理 Content 变化后的逻辑
// 例如:
label.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(newContent));
#if DEBUG
label.Foreground = GetInverseBrush(new SolidColorBrush((Color)ColorConverter.ConvertFromString(newContent)));
#else
label.Foreground = new SolidColorBrush(Colors.Transparent);
#endif
}
}
}
public static class LabelContentChangedBehavior
{
public static readonly DependencyProperty EnableContentChangedProperty =
DependencyProperty.RegisterAttached(
"EnableContentChanged",
typeof(bool),
typeof(LabelContentChangedBehavior),
new PropertyMetadata(false, OnEnableContentChangedChanged));
// 定义一个附加事件
public static readonly RoutedEvent ContentChangedEvent =
EventManager.RegisterRoutedEvent(
"ContentChanged",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(LabelContentChangedBehavior));
// 添加和移除事件的附加方法
public static void AddContentChangedHandler(DependencyObject d, RoutedEventHandler handler)
{
if (d is UIElement uiElement)
{
uiElement.AddHandler(ContentChangedEvent, handler);
}
}
public static void RemoveContentChangedHandler(DependencyObject d, RoutedEventHandler handler)
{
if (d is UIElement uiElement)
{
uiElement.RemoveHandler(ContentChangedEvent, handler);
}
}
public static bool GetEnableContentChanged(DependencyObject obj)
{
return (bool)obj.GetValue(EnableContentChangedProperty);
}
public static void SetEnableContentChanged(DependencyObject obj, bool value)
{
obj.SetValue(EnableContentChangedProperty, value);
}
private static void OnEnableContentChangedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is Label label)
{
if ((bool)e.NewValue)
{
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(
Label.ContentProperty, typeof(Label));
if (dpd != null)
{
dpd.AddValueChanged(label, OnLabelContentChanged);
}
}
else
{
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(
Label.ContentProperty, typeof(Label));
if (dpd != null)
{
dpd.RemoveValueChanged(label, OnLabelContentChanged);
}
}
}
}
private static void OnLabelContentChanged(object sender, EventArgs e)
{
if (sender is Label label)
{
var newContent = label.Content?.ToString();
// 创建一个新的 RoutedEventArgs,并设置源为当前 Label
RoutedEventArgs args = new RoutedEventArgs(ContentChangedEvent, label);
label.RaiseEvent(args);
}
}
}