ProgressBar with percent text
09/03/2020
Aucun commentaire
If you want a progress bar with text two colors you can use this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
public class ProgressBarText : ProgressBar { static ProgressBarText() { DefaultStyleKeyProperty.OverrideMetadata(typeof(ProgressBarText), new FrameworkPropertyMetadata(typeof(ProgressBarText))); } #region IndicatorColor (DP SHORT) public Brush IndicatorColor { get { return (Brush)GetValue(IndicatorColorProperty); } set { SetValue(IndicatorColorProperty, value); } } public static readonly DependencyProperty IndicatorColorProperty = DependencyProperty.Register("IndicatorColor", typeof(Brush), typeof(ProgressBarText), new FrameworkPropertyMetadata(Brushes.Blue, FrameworkPropertyMetadataOptions.AffectsRender)); #endregion #region IndicatorTextColor (DP SHORT) public Brush IndicatorTextColor { get { return (Brush)GetValue(IndicatorTextColorProperty); } set { SetValue(IndicatorTextColorProperty, value); } } public static readonly DependencyProperty IndicatorTextColorProperty = DependencyProperty.Register("IndicatorTextColor", typeof(Brush), typeof(ProgressBarText), new FrameworkPropertyMetadata(Brushes.White, FrameworkPropertyMetadataOptions.AffectsRender)); #endregion #region TextColor (DP SHORT) public Brush TextColor { get { return (Brush)GetValue(TextColorProperty); } set { SetValue(TextColorProperty, value); } } public static readonly DependencyProperty TextColorProperty = DependencyProperty.Register("TextColor", typeof(Brush), typeof(ProgressBarText), new FrameworkPropertyMetadata(Brushes.Black, FrameworkPropertyMetadataOptions.AffectsRender)); #endregion } public class ProgressBarTextConverter : MarkupExtension, IMultiValueConverter { public override object ProvideValue(IServiceProvider serviceProvider) { return this; } public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { try { double total = (double)values[0]; double size = (double)values[1]; double calc= (size * 100 / total); Console.WriteLine($"CALCUL = {calc}"); int v = (int)Math.Round(calc); return $"{v} %"; } catch { } return null; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } |
and style in generic.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<Style TargetType="{x:Type local:ProgressBarText}"> <Setter Property="BorderBrush" Value="gray" /> <Setter Property="BorderThickness" Value="0.5" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:ProgressBarText}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <Grid> <Grid x:Name="PART_Track"> <TextBlock x:Name="PART_Text" Grid.Column="1" Foreground="{TemplateBinding TextColor}" VerticalAlignment="Center" HorizontalAlignment="Center"> <TextBlock.Text> <MultiBinding Converter="{local:ProgressBarTextConverter}"> <Binding Path="ActualWidth" ElementName="PART_Track" /> <Binding Path="ActualWidth" ElementName="PART_Indicator" /> </MultiBinding> </TextBlock.Text></TextBlock> </Grid> <Border Background="{TemplateBinding IndicatorColor}" VerticalAlignment="Stretch" HorizontalAlignment="Left" x:Name="PART_Indicator"> <Grid Width="{Binding ActualWidth, ElementName=PART_Track}"> <TextBlock Text="{Binding Text, ElementName=PART_Text}" Foreground="{TemplateBinding IndicatorTextColor}" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" /> </Grid> </Border> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> |
result for
code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<local:ProgressBarText Margin="10,112,9.6,0" Height="41" TextBlock.FontSize="24" TextBlock.FontWeight="Bold" TextColor="red" IndicatorTextColor="Black" VerticalAlignment="Top" Value="49" IndicatorColor="Lime" /> <local:ProgressBarText Margin="10,158,9.6,0" Height="41" TextBlock.FontSize="24" TextBlock.FontWeight="Bold" VerticalAlignment="Top" Value="49" /> |
Categories: Non classé
Commentaires récents