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 { /// /// GroupTextBoxControl.xaml 的交互逻辑 /// public partial class GroupTextBoxControl : UserControl { #region 属性 public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(GroupTextBoxControl), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender)); /// /// 标题 /// public string Title { get { return (string)this.GetValue(TitleProperty); } set { this.SetValue(TitleProperty, value); } } public static readonly DependencyProperty UnitProperty = DependencyProperty.Register("Unit", typeof(string), typeof(GroupTextBoxControl), new FrameworkPropertyMetadata("deg", FrameworkPropertyMetadataOptions.AffectsRender)); /// /// 单位 /// public string Unit { get { return (string)this.GetValue(UnitProperty); } set { this.SetValue(UnitProperty, value); } } public static readonly DependencyProperty IntValueProperty = DependencyProperty.Register("IntValue", typeof(int), typeof(GroupTextBoxControl), new PropertyMetadata(-1, new PropertyChangedCallback(IntValueChanged))); /// /// 数值 /// public int IntValue { get { return (int)this.GetValue(IntValueProperty); } set { this.SetValue(IntValueProperty, value); } } public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register("MinValue", typeof(int), typeof(GroupTextBoxControl), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender)); /// /// 最小数值 /// public int MinValue { get { return (int)this.GetValue(MinValueProperty); } set { this.SetValue(MinValueProperty, value); } } public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(int), typeof(GroupTextBoxControl), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender)); /// /// 最大数值 /// public int MaxValue { get { return (int)this.GetValue(MaxValueProperty); } set { this.SetValue(MaxValueProperty, value); } } public static readonly DependencyProperty EnableProperty = DependencyProperty.Register("Enable", typeof(bool), typeof(GroupTextBoxControl), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender)); /// /// 可用性 /// public bool Enable { get { return (bool)this.GetValue(EnableProperty); } set { this.SetValue(EnableProperty, value); } } public static readonly DependencyProperty ValidResultProperty = DependencyProperty.Register("ValidResult", typeof(bool), typeof(GroupTextBoxControl), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender)); /// /// 合理性 /// public bool ValidResult { get { return (bool)this.GetValue(ValidResultProperty); } set { this.SetValue(ValidResultProperty, value); } } /// /// 数值发生变化事件 /// /// /// private static void IntValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if(e.NewValue!=null) { int newValue = (int)e.NewValue; int minValue=(int)d.GetValue(MinValueProperty); int maxValue= (int)d.GetValue(MaxValueProperty); bool validResult= newValue >= minValue && newValue <= maxValue; d.SetValue(ValidResultProperty, validResult); } } #endregion public GroupTextBoxControl() { InitializeComponent(); } protected override void OnRender(DrawingContext drawingContext) { base.OnRender(drawingContext); ValidResult = IntValue >= MinValue && IntValue <= MaxValue; RectangleSetPoint.Fill = ValidResult? new SolidColorBrush(Colors.Transparent) : new SolidColorBrush(Colors.Yellow); txtValue.Background = ValidResult? new SolidColorBrush(Colors.Transparent) : new SolidColorBrush(Colors.Yellow); } } }