为什么ProgressBar.SetValue使用有效值抛出ArgumentException?

Dro*_*onz 1 c# wpf xaml argumentexception progress-bar

我正在使用WPF的ProgressBar对象,它具有Value属性,我可以传递一个常量的int或十进制值,它不会抱怨,但如果我传递一个float(或int或string)变量值,它会barfs一个ArgumentException(例如"Message='0.02380952' is not a valid value for property 'Value'.")在我身上,这对我来说似乎是荒谬的,让我难过.我跟随的MS示例使用常量浮点值.然而,MS文档对Value属性的引用表示它是一个int,这似乎是错误的,因为我可以将它传递给常量.1或.5并获得正确的行为,而我的MS示例使用0和1以及最小值和最大值.那么,我需要做什么呢?

我的代码:

XAML:

<ProgressBar x:Name="prgProgress" Width="200" Height="20" Minimum="0" Maximum="100" />
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

H.B*_*.B. 5

您链接到Windows窗体等效,在WPF中它是一个double,这意味着如果您使用,SetValue您将需要使用该确切类型,因为没有隐式转换.

来自SetValue:

如果提供的类型与最初注册的依赖项属性声明的类型不匹配,则抛出异常.应始终将value参数提供为适当的类型.

(为什么不使用包装器?ie prgProgress.Value = fracDone)