Binding Run inside Textblock results in exception in WPF
Asked Answered
A

1

15

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>
Awe answered 30/5, 2015 at 21:3 Comment(0)
T
35

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}"/>
Td answered 30/5, 2015 at 21:8 Comment(7)
I believe, by default, binding mode of Run element is one way only. Correct me if I'm wrong.Renick
Thanks dkozl, actually changing the mode to OneWay solved the problem.Awe
@SriramSakthivel no Run by default binds Text both ways and it will cause exception if property doesn't have public setter.Td
But I'm surprised by XamlParseException. Such a misleading exception isn't it? It suggests that xaml is wrong. I have no Ide to check this at the moment.Renick
There is a inner exception that explains the problemTd
@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?Stylographic
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?Yeargain

© 2022 - 2024 — McMap. All rights reserved.