电池 SimpleBattery


xaml


    


    
        
        
    
    
        
            
            
            
            
            
        
        
    
    
        
            
                
                    
                    
                    
                
            
        
    

cs

/// 
/// SimpleBattery.xaml 的交互逻辑
/// 
public partial class SimpleBattery : UserControl
{
    public SimpleBattery()
    {
        InitializeComponent();
    }
    /// 
    /// 描边颜色
    /// 
    public Brush RimStroke
    {
        get => (Brush)GetValue(RimStrokeProperty);
        set => SetValue(RimStrokeProperty, value);
    }
    public static readonly DependencyProperty RimStrokeProperty =
        DependencyProperty.Register("RimStroke", typeof(Brush), typeof(SimpleBattery), new PropertyMetadata(default(Brush)));
    /// 
    /// 背景颜色
    /// 
    public Brush BackgroundStroke
    {
        get => (Brush)GetValue(BackgroundStrokeProperty);
        set => SetValue(BackgroundStrokeProperty, value);
    }
    public static readonly DependencyProperty BackgroundStrokeProperty =
        DependencyProperty.Register("BackgroundStroke", typeof(Brush), typeof(SimpleBattery), new PropertyMetadata(default(Brush)));
    /// 
    /// 范围值颜色
    /// 
    public Brush RangeStroke
    {
        get => (Brush)GetValue(RangeStrokeProperty);
        set => SetValue(RangeStrokeProperty, value);
    }
    public static readonly DependencyProperty RangeStrokeProperty =
        DependencyProperty.Register("RangeStroke", typeof(Brush), typeof(SimpleBattery), new PropertyMetadata(default(Brush)));

    /// 
    /// 范围最大值
    /// 
    public double Maximum
    {
        get => (double)GetValue(MaximumProperty);
        set => SetValue(MaximumProperty, value);
    }
    public static readonly DependencyProperty MaximumProperty =
        DependencyProperty.Register("Maximum", typeof(double), typeof(SimpleBattery), new PropertyMetadata(100d));


    /// 
    /// 范围真值 
    /// 
    public double ProgressValue
    {
        get => (double)GetValue(ProgressValueProperty);
        set => SetValue(ProgressValueProperty, value);
    }
    public static readonly DependencyProperty ProgressValueProperty =
        DependencyProperty.Register("ProgressValue", typeof(double), typeof(SimpleBattery), new PropertyMetadata(50d));


}

internal class WithConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.Length == 3 && values[0] is Grid grid && values[1] is double max && values[2] is double val)
        {
            var width = (grid.ColumnDefinitions[0].ActualWidth - 2) * val / max;
            return width;
        }
        return DependencyProperty.UnsetValue;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

demo


prism


WPF