我将 ProgressBar 值属性绑定(bind)到 ViewModel 的只读属性。这在 Win 7 机器上工作正常,但在 Win XP 机器上,它会抛出异常。
XAML
<ProgressBar Grid.Row="0" Minimum="0.0" Maximum="1.0" Value="{Binding Progress, Mode=TwoWay}" />
异常
An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
Additional information: A TwoWay or OneWayToSource binding cannot work on the read-only property 'Progress'
来自异常消息和WPF Binding default mode邮政。解决办法好像是设置绑定(bind)模式为OneWay。
我的问题是是什么导致进度条设置值?为什么它可以在 Win 7 上运行而不能在 XP 上运行?
最佳答案
双向绑定(bind)意味着目标(本例中为 ProgressBar.Value
)应将其更改传播回源(Progress
属性)。如果源是只读的,这当然是不可能的,因此异常(exception)是有道理的。正如您所指出的,唯一有意义的绑定(bind)是源到目标的单向绑定(bind)。
我不知道为什么它会在 Win 7 上工作 - 尽管大概“工作”只是意味着它默默地失败,而不是实际更新只读属性。
关于wpf - ProgressBar 值绑定(bind)到 ReadOnly 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19170033/