为什么 ProgressBar.SetValue 会抛出带有有效值的 ArgumentException ?

发布于 2024-12-31 21:07:45 字数 1368 浏览 0 评论 0原文

我正在使用 WPF 的 ProgressBar 对象,它有一个 Value 属性,我可以向它传递一个常量 int 或十进制值,它不会抱怨,但如果我向它传递一个 float(或 int 或 string)变量值,它会抛出一个ArgumentException (例如 "Message='0.02380952' 不是属性 'Value' 的有效值。")对我来说似乎很荒谬,并且让我难住了。我下面的 MS 示例 使用常量浮点值。 对 Value 属性的 MS 文档引用 然而说它是一个 int,这似乎是错误的,因为我可以传递它常量 .1 或 .5 并获得正确的行为,而我的 MS 示例使用 0 和 1 以及最小值和最大值。那么,我需要在这里做什么?

我的代码:

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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沦落红尘 2025-01-07 21:07:45

您链接到等效的 Windows 窗体 WPF 它是双精度型,这意味着如果您使用 SetValue,您将需要使用该确切类型,因为没有隐式转换。

来自 SetValue

如果提供的类型与最初注册时为依赖属性声明的类型不匹配,则会引发异常。应始终以适当的类型提供值参数。

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

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文