17

I'm trying to bind two <Run>s inside a TextBlock as shown in the snippet below. But I'm getting an XamlParseException.

Basically I'm trying to achieve this format:

CodeNum: LongDescription

If the below code is doomed to fail what other alternatives do I have?

<TextBlock>
    <Run FontWeight="Bold" Text="{Binding CodeNum}"/>
    <Run FontWeight="Bold" Text=": "/>
    <Run Text="{Binding LongDescription}"/>
</TextBlock>
0

1 Answer 1

37

I'm guessing that either LongDescription or CodeNumis is a read-only property (doesn't have public setter). You need to change binding to be one way for all read-only properties that you use in Run

<Run Text="{Binding LongDescription, Mode=OneWay}"/>
Sign up to request clarification or add additional context in comments.

7 Comments

I believe, by default, binding mode of Run element is one way only. Correct me if I'm wrong.
Thanks dkozl, actually changing the mode to OneWay solved the problem.
@SriramSakthivel no Run by default binds Text both ways and it will cause exception if property doesn't have public setter.
@Sriram Sakthivel Lucky you, my application just went into break mode without telling me ANYthing about the source of the exception. I'm just glad it was the only thing I recently changed so I knew binding my run to an expression-bodied property was at fault. Searching for the reason why was painful enough. I just wonder now, why is it like that? I can bind to the Text property of the TextBlock just fine without a setter, why does Run behave different?
Can anyone explain (or point to the relevant docs for) the reasoning why Run has a default Mode=TwoWay? I mean, is the user able to change the text I bind to a Run in any way? The way I see it, Run is just a way to bind different snippets of read-only text within a single TextBlock. Am I wrong?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.