1

I am using WPF's ProgressBar object, which has a Value property, and I can pass it a constant int or decimal value and it doesn't complain, but if I pass it a float (or int or string) variable value, it barfs an ArgumentException (e.g. "Message='0.02380952' is not a valid value for property 'Value'.") at me, which just seems preposterous to me, and has me stumped. The MS example I am following uses constant float values. The MS doc reference to the Value property however says it is an int, which seems just wrong since I can pass it constant .1 or .5 and get the right behavior, and my MS example uses 0 and 1 and the min and max values. So, what do I need to do here?

My code:

xaml:

<ProgressBar x:Name="prgProgress" Width="200" Height="20" Minimum="0" Maximum="100" />

c#:

float numMems = ListToShow.Count; 
float numDone = 0.0f;
int fracDone = 0;
string sProgress = "0%";
foreach (RAM_Member mem in ListToShow)
{
    if (isCanceled == true) break;
    mem.CalculateValues();
    numDone++;
    fracDone = (int)(100.0 * numDone / numMems);
    sProgress = (100 * numDone / numMems).ToString("0.") + "%";
    Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate { prgProgress.SetValue(ProgressBar.ValueProperty, fracDone); }, null);
    Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate { txtProgress.SetValue(TextBlock.TextProperty, sProgress); }, null);
}
H.B.
  • 166,899
  • 29
  • 327
  • 400
Dronz
  • 1,970
  • 4
  • 27
  • 50

1 Answers1

5

You linked to the windows form equivalent, in WPF it's a double, which means that if you use SetValue you will need to use that exact type as there is no implicit conversion.

From SetValue:

If the provided type does not match the type that is declared for the dependency property as it was originally registered, an exception is thrown. The value parameter should always be provided as the appropriate type.

(Why don't you use the wrapper instead? i.e. prgProgress.Value = fracDone)

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Wow! You are correct! I thought it might want double for a moment, but I ran out of endurance trying things before getting around to trying that. But you are right. THANK YOU! – Dronz Jan 31 '12 at 03:42
  • As for why I am not using the wrapper, I am following the MS example code, and I thought it was needed ... but I tried switching to prgProgress.Value = fracDone; and it works, so I'll have to go re-watch the explanation video to see what the guy said - he was quite deliberate about using it but it doesn't seem to be needed. – Dronz Jan 31 '12 at 03:48
  • 1
    @Dronz: Should not make any difference at all, as wrapper properties such as `Value` internally call `SetValue`, the convenient thing is that you can pass other types into the property and the value will be converted to the property type implicitly if such a conversion exists. – H.B. Jan 31 '12 at 03:53
  • Thanks for explaining that. I will go watch the video again later, and post if there is an explanation why it was used. – Dronz Jan 31 '12 at 03:55
  • I gave it another listen, and he says it is to call the value directly, but he doesn't say why. Earlier, he had just set the value with Value =, but when he switched to use a dispatcher call, he also changed to use SetValue, mentioning it would set it directly, but not why. – Dronz Jan 31 '12 at 06:36