using System.Windows; using System.Windows.Media; using UserControl = System.Windows.Controls.UserControl; using Brush = System.Windows.Media.Brush; using Brushes = System.Windows.Media.Brushes; using Color = System.Windows.Media.Color; namespace OperationControl.Controls { /// /// Interaction logic for ProgressBarText.xaml /// public partial class ProgressBarText : UserControl { public ProgressBarText() { InitializeComponent(); SizeChanged += (_, __) => InvalidateVisual(); UpdateDisplayText(); } // ---- DP básicos ---- public double Minimum { get => (double)GetValue(MinimumProperty); set => SetValue(MinimumProperty, value); } public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register(nameof(Minimum), typeof(double), typeof(ProgressBarText), new PropertyMetadata(0d, OnAnyChange)); public double Maximum { get => (double)GetValue(MaximumProperty); set => SetValue(MaximumProperty, value); } public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register(nameof(Maximum), typeof(double), typeof(ProgressBarText), new PropertyMetadata(100d, OnAnyChange)); public double Value { get => (double)GetValue(ValueProperty); set => SetValue(ValueProperty, value); } public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(nameof(Value), typeof(double), typeof(ProgressBarText), new PropertyMetadata(0d, OnAnyChange)); // Se Text estiver vazio, usa ValueFormat com Value public string Text { get => (string)GetValue(TextProperty); set => SetValue(TextProperty, value); } public static readonly DependencyProperty TextProperty = DependencyProperty.Register(nameof(Text), typeof(string), typeof(ProgressBarText), new PropertyMetadata(null, OnAnyChange)); public string ValueFormat { get => (string)GetValue(ValueFormatProperty); set => SetValue(ValueFormatProperty, value); } public static readonly DependencyProperty ValueFormatProperty = DependencyProperty.Register(nameof(ValueFormat), typeof(string), typeof(ProgressBarText), new PropertyMetadata("{0:0}%", OnAnyChange)); public Brush BarBrush { get => (Brush)GetValue(BarBrushProperty); set => SetValue(BarBrushProperty, value); } public static readonly DependencyProperty BarBrushProperty = DependencyProperty.Register(nameof(BarBrush), typeof(Brush), typeof(ProgressBarText), new PropertyMetadata((Brush)new SolidColorBrush(Color.FromRgb(0x2E, 0xCC, 0x71)))); public Brush BackgroundBrush { get => (Brush)GetValue(BackgroundBrushProperty); set => SetValue(BackgroundBrushProperty, value); } public static readonly DependencyProperty BackgroundBrushProperty = DependencyProperty.Register(nameof(BackgroundBrush), typeof(Brush), typeof(ProgressBarText), new PropertyMetadata((Brush)new SolidColorBrush(Color.FromRgb(0x22, 0x22, 0x22)))); public Brush BorderBrush { get => (Brush)GetValue(BorderBrushProperty); set => SetValue(BorderBrushProperty, value); } public static readonly DependencyProperty BorderBrushProperty = DependencyProperty.Register(nameof(BorderBrush), typeof(Brush), typeof(ProgressBarText), new PropertyMetadata((Brush)new SolidColorBrush(Color.FromRgb(0x44, 0x44, 0x44)))); public Brush TextBrush { get => (Brush)GetValue(TextBrushProperty); set => SetValue(TextBrushProperty, value); } public static readonly DependencyProperty TextBrushProperty = DependencyProperty.Register(nameof(TextBrush), typeof(Brush), typeof(ProgressBarText), new PropertyMetadata(Brushes.White)); public double CornerRadius { get => (double)GetValue(CornerRadiusProperty); set => SetValue(CornerRadiusProperty, value); } public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register(nameof(CornerRadius), typeof(double), typeof(ProgressBarText), new PropertyMetadata(4d)); // Exposto apenas para binding interno public string DisplayText { get => (string)GetValue(DisplayTextProperty); private set => SetValue(DisplayTextPropertyKey, value); } private static readonly DependencyPropertyKey DisplayTextPropertyKey = DependencyProperty.RegisterReadOnly(nameof(DisplayText), typeof(string), typeof(ProgressBarText), new PropertyMetadata("")); public static readonly DependencyProperty DisplayTextProperty = DisplayTextPropertyKey.DependencyProperty; private static void OnAnyChange(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((ProgressBarText)d).UpdateDisplayText(); } private void UpdateDisplayText() { if (!string.IsNullOrWhiteSpace(Text)) { DisplayText = Text; return; } var span = Maximum - Minimum; var v = span <= 0 ? 0 : (Value - Minimum) / span; DisplayText = string.Format(ValueFormat ?? "{0:0}%", v * 100.0); } } }