Frage

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);
}
War es hilfreich?

Lösung

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)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top