问题
in one of my apps I have a code like this:
<ProgressBar Grid.Column="0" Grid.Row="0" HorizontalAlignment="Stretch" Height="27" Margin="5,0,5,0" Maximum="{Binding TabuProgressEnd}" Value="{Binding TabuProgress}" />
While I was testing this everything is ok, but when my client opened this under VS and run this code threw an exception:
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 'TabuProgress' of type 'TSPLib.TabuEngine'.
Usually I would think this is some kind of hoax, but I know that the guy has no idea about coding and making the "Mode=OneWay" explicit helped. How is it possible that the default binding mode differs on different machines?
回答1:
The Value
property in ProgressBar
binds TwoWay
by default so the exception should occur unless you explicitly set Mode
to OneWay
. However I can't explain why it doesn't occur on your machine. I tried using Reflector with .NET versions 4.0, 3.5 and 3.0 and as far as I can tell, the default binding mode hasn't changed in a while.
If you have Reflector installed, it would be interesting to see what the ValueProperty (inherited from RangeBase) looks like on your machine
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(
"Value",
typeof(double),
typeof(RangeBase),
new FrameworkPropertyMetadata(
0.0,
FrameworkPropertyMetadataOptions.Journal |
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
new PropertyChangedCallback(RangeBase.OnValueChanged),
new CoerceValueCallback(RangeBase.ConstrainToRange)),
new ValidateValueCallback(RangeBase.IsValidDoubleValue));
来源:https://stackoverflow.com/questions/4759706/wpf-binding-default-mode