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 CyberX8_Themes.UserControls { /// /// PressureBlockControl.xaml 的交互逻辑 /// public partial class PressureBlockControl : UserControl { #region 属性 public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(PressureBlockControl), new FrameworkPropertyMetadata((double)0, FrameworkPropertyMetadataOptions.AffectsRender)); /// /// 数值 /// public double Value { get { return (double)this.GetValue(ValueProperty); } set { this.SetValue(ValueProperty, value); } } public static readonly DependencyProperty ForeColorProperty = DependencyProperty.Register("ForeColor", typeof(SolidColorBrush), typeof(PressureBlockControl), new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Lime), FrameworkPropertyMetadataOptions.AffectsRender)); /// /// 文字颜色 /// public SolidColorBrush ForeColor { get { return (SolidColorBrush)this.GetValue(ForeColorProperty); } set { this.SetValue(ForeColorProperty, value); } } public static readonly DependencyProperty BackgroundColorProperty = DependencyProperty.Register("BackgroundColor", typeof(SolidColorBrush), typeof(PressureBlockControl), new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Black), FrameworkPropertyMetadataOptions.AffectsRender)); /// /// 背景颜色 /// public SolidColorBrush BackgroundColor { get { return (SolidColorBrush)this.GetValue(BackgroundColorProperty); } set { this.SetValue(BackgroundColorProperty, value); } } public static readonly DependencyProperty IsWarningProperty = DependencyProperty.Register("IsWarning", typeof(bool), typeof(PressureBlockControl), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(IsWarningPropertyChanged))); /// /// 是否告警 /// public bool IsWarning { get { return (bool)this.GetValue(IsWarningProperty); } set { this.SetValue(IsWarningProperty, value); } } public static readonly DependencyProperty IsErrorProperty = DependencyProperty.Register("IsError", typeof(bool), typeof(PressureBlockControl), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(IsErrorPropertyChanged))); /// /// 是否错误 /// public bool IsError { get { return (bool)this.GetValue(IsErrorProperty); } set { this.SetValue(IsErrorProperty, value); } } private static void IsWarningPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if(e.NewValue!=null) { bool isWarning=(bool)e.NewValue; bool isError = (bool)d.GetValue(IsErrorProperty); if(isWarning) { if (!isError) { d.SetValue(BackgroundColorProperty, new SolidColorBrush(Colors.Yellow)); d.SetValue(ForeColorProperty, new SolidColorBrush(Colors.Black)); } } else { if (!isError) { d.SetValue(BackgroundColorProperty, new SolidColorBrush(Colors.Black)); d.SetValue(ForeColorProperty, new SolidColorBrush(Colors.Lime)); } } } } private static void IsErrorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (e.NewValue != null) { bool isError = (bool)e.NewValue; bool isWarning = (bool)d.GetValue(IsWarningProperty); if (isError) { d.SetValue(BackgroundColorProperty, new SolidColorBrush(Colors.Red)); d.SetValue(ForeColorProperty, new SolidColorBrush(Colors.Black)); } else { if (!isWarning) { d.SetValue(BackgroundColorProperty, new SolidColorBrush(Colors.Black)); d.SetValue(ForeColorProperty, new SolidColorBrush(Colors.Lime)); } } } } #endregion public PressureBlockControl() { InitializeComponent(); } } }