In .NET 4.0, Run.Text is bindab开发者_高级运维le. So I tried to bind it:
<Run Text="{Binding DisplayText}"/>
But when I ran, I got an error: "A TwoWay or OneWayToSource binding cannot work on the read-only property 'DisplayText' of type 'SomeNamespace.SomeClass'."
My DisplayText property was indeed read-only, but so is a Run -- Runs go in in TextBlocks, which you can't edit. So why would I be getting this error? I dug into PresentationFramework with dotPeek and sure enough:
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof (string), typeof (Run),
(PropertyMetadata) new FrameworkPropertyMetadata((object) string.Empty,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
new PropertyChangedCallback(Run.OnTextPropertyChanged),
new CoerceValueCallback(Run.CoerceText)));
The fourth line, plain as day, specifies that Run.Text should bind two-way by default, which makes no sense and seems like a glaring design bug.
Of course, this is easy enough to work around:
<Run Text="{Binding DisplayText, Mode=OneWay}"/>
But why should I have to work around it? Why does Run bind two-way by default?
Just a guess here:
It might be because Run
objects are also used in the RichTextBox
control, and I can imagine this control might want to Bind TwoWay
by default!
精彩评论