WPF绑定默认模式

时间:2011-01-21 14:07:16

标签: wpf binding

在我的某个应用中,我有这样的代码:

<ProgressBar Grid.Column="0" Grid.Row="0" HorizontalAlignment="Stretch" Height="27" Margin="5,0,5,0" Maximum="{Binding TabuProgressEnd}" Value="{Binding TabuProgress}" />

当我测试这一切时,一切正常,但是当我的客户端在VS下打开它并运行此代码时抛出异常:

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'.

通常我会认为这是某种骗局,但我知道这个家伙不知道编码并使“Mode = OneWay”明确有帮助。不同机器上的默认绑定模式有何不同?

1 个答案:

答案 0 :(得分:4)

Value中的ProgressBar属性默认绑定TwoWay,因此除非您明确将Mode设置为OneWay,否则应发生异常。但是,我无法解释为什么它不会在您的机器上发生。我尝试使用.NET版本4.0,3.5和3.0的Reflector,据我所知,默认绑定模式暂时没有改变。

如果您安装了Reflector,那么看看ValueProperty(继承自RangeBase)在您的计算机上是什么样子会很有趣

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