Textblock 内的绑定运行导致 WPF 中的异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30551372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:10:03  来源:igfitidea点击:

Binding Run inside Textblock results in exception in WPF

c#wpfxamltextblock

提问by Vahid

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

我正在尝试将两个<Run>s绑定到 a 中TextBlock,如下面的代码段所示。但我得到了一个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>

回答by dkozl

I'm guessing that either LongDescriptionor CodeNumisis 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

我猜,要么LongDescription或者CodeNumis是一个只读属性(没有公共的setter)。您需要将绑定更改为您在其中使用的所有只读属性的一种方式Run

<Run Text="{Binding LongDescription, Mode=OneWay}"/>