XAML中的TextBlock和Run绑定

25

我有一个问题,就是Run的绑定不起作用。这是我的当前代码。

<TextBlock
  x:Name="txtCompanyName"
  Text="{Binding Path=SelectedItem.CompanyName, ElementName=lbSourceList}"
  Foreground="White"
  FontSize="18.667"
  Height="33.667" 
  Margin="10,-0.5,0,-1.5">
  <Run Text=" : " Foreground="White"/>
  <Run Text=" "/>
  <Run Text=" " Foreground="White"/>
  <Run Text=" "/>
  <Run Text="{Binding Path=SelectedItem.RFQID, ElementName=lbSourceList}" />
</TextBlock>
我看到公司名字了,但是额外的数据没有显示出来。你有什么想法为什么这种绑定失败了?

替代答案和最终答案

<TextBlock TextWrapping="Wrap" 
           Text="{Binding RFQID}" 
            FontWeight="Bold" 
            Foreground="#FFFFF504" 
            HorizontalAlignment="Left" Width="185"> 
            <Run Text=" ~ "/> 
            <Run Text="{Binding RFQNo}" FontWeight="Bold" Foreground="#FFFFF504"/> 
            <Run Text=" ~ "/> 
            <Run Text="{Binding Status}" FontWeight="Bold" 
                                         Foreground="#FF85F35F"/>
 </TextBlock>

展示你的绑定错误... - H.B.
谢谢。没有显示绑定错误。 - scottsanpedro
自WPF 4.0以来,您可以在运行时使用绑定。 - Chepene
2个回答

33

您不能同时使用Inlines(即Run子节点)和TextBlock.Text


1
是的,你说得对。问题已经解决了。然而,以下代码实际上可以正常工作:<TextBlock TextWrapping="Wrap" Text="{Binding RFQID}" FontWeight="Bold" Foreground="#FFFFF504" HorizontalAlignment="Left" Width="185"> <Run Text=" ~ "/> <Run Text="{Binding RFQNo}" FontWeight="Bold" Foreground="#FFFFF504"/> <Run Text=" ~ "/> <Run Text="{Binding Status}" FontWeight="Bold" Foreground="#FF85F35F"/> </TextBlock> - scottsanpedro

0
另外:请确保绑定值为 String
另一个可能的原因是,如果您发现绑定在<TextBlock Text="{Binding...}" /> 中有效,但在<TextBlock><Run Text="{Binding...}"...相同的绑定失败<TextBlock Text=... 绑定隐式调用非字符串值的 ToString() 方法,但 <Run Text=... 不会这样做。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接