ProgressBar not updating on change to Maximum through binding

后端 未结 2 1195
甜味超标
甜味超标 2021-01-21 05:36


        
相关标签:
2条回答
  • 2021-01-21 06:19

    It looks like the bar size is calculated in the private method ProgressBar.SetProgressBarIndicatorLength. It's only called from OnValueChanged, OnTrackSizeChanged, and OnIsIndeterminateChanged.

    You could call SetProgressBarIndicatorLength through reflection, or cycle one of the properties that causes it to be called. This is lame, but it doesn't look like the ProgressBar was designed so that the Maximum and Minimum would be changed in mid-progress.

    Regardless of which method you choose, you can figure out when the Maximum property changes by using DependencyPropertyDescriptor.AddValueChanged:

    DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(ProgressBar.MaximumProperty, typeof(ProgressBar)));
    if (dpd != null)
    {
       dpd.AddValueChanged(myProgressBar, delegate
       {
          // handle Maximum changes here
       });
    }
    
    0 讨论(0)
  • 2021-01-21 06:24

    I was having trouble getting the solution here to work, but I found another work around. My progress bar wouldn't update as I changed the datasource of my object (11 of 11 would change to 10 of 10 and freeze the progress bar), and realized that I didn't need to update maximum value at all.

    Instead I used a converter on the value to convert it to a percent, and set my maximum at 100. The result displays the same, but without the bug for changing maximum value.

    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}}" />
    
    0 讨论(0)
提交回复
热议问题