ProgressBar does not update when switching to maximum binding

<ProgressBar Foreground="Red"
             Background="Transparent"
             Value="{Binding NumFailed, Mode=OneWay}"
             Minimum="0"
             Maximum="{Binding NumTubes, Mode=OneWay, Converter={x:Static wpftools:DebuggingConverter.Instance}, ConverterParameter=Failedprogressbar}"
             FlowDirection="RightToLeft"
             Style="{DynamicResource {x:Static wpftools:CustomResources.StyleProgressBarVistaKey}}" />

This is what my progress bar looks like. The style came from http://mattserbinski.com/blog/look-and-feel-progressbar , and DebuggingConverter is a contactless converter that prints the value, type and parameter for the Console. I checked that the converter for Maximum is called when the NumTubes property changes.

Basically, the ProgressBar will not be redrawn until the value changes. So, if I have 2 tubes and 1, this will fail, even if I add another 20 tubes, the panel will still be half full until NumFailed changes, then this proportion will be updated. I tried to add false notifications about the NumFailed property, but this does not seem to work as the value has not changed.

Ideas?

+3
source share
2 answers

It looks like the size of the bar is calculated in a private method ProgressBar.SetProgressBarIndicatorLength. It is called only from OnValueChanged, OnTrackSizeChangedand OnIsIndeterminateChanged.

SetProgressBarIndicatorLength , . , , ProgressBar , Maximum Minimum .

, , , Maximum DependencyPropertyDescriptor.AddValueChanged:

DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(ProgressBar.MaximumProperty, typeof(ProgressBar)));
if (dpd != null)
{
   dpd.AddValueChanged(myProgressBar, delegate
   {
      // handle Maximum changes here
   });
}
+4

, . , (11 11 10 10 ), , .

, , 100. , .

public class CreatureStaminaConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var creature = (CreatureBase.CreatureEntityData) value;
        double max = creature.entityData.MaxStat;
        return creature.CurrentStamina/max*100;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

 <ProgressBar Name="rpbStamina" Minimum="0" Maximum="100" Value="{Binding entityData, Converter={StaticResource CreatureStaminaConverter}}" />
+2

Source: https://habr.com/ru/post/1704917/


All Articles