【问题标题】:Unable to set Binding to a Run that resides in a TextBlock无法将绑定设置为驻留在 TextBlock 中的运行
【发布时间】:2010-12-19 02:59:19
【问题描述】:
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="This is my text">
  <TextBlock>
    <Run Text="{Binding}"/>
  </TextBlock>
</Window>

抛出 InvalidOperationException:“双向绑定需要 Path 或 XPath。”

指定Mode=OneWay,会导致奇怪的编译器错误:

The tag 'Binding,' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.

有什么方法可以解决这个问题吗?

【问题讨论】:

    标签: wpf binding textblock bindable


    【解决方案1】:

    我还没有找到原因,但是你可以这样做而不会让它变得太尴尬:

    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="This is my text">
      <TextBlock>
        <Run Text="{Binding Path=.}"/>
      </TextBlock>
    </Window>
    

    由于某种原因

    <Run Text="{Binding}" />
    

    导致运行时错误,但是

    <Run Text="{Binding Path=.}" />
    

    没有。原因可能与您对绑定“模棱两可”有关,某些后备行为会接管来解释您的绑定。或者,这是一个真正的 MS 错误,在 Run 控件上解释了 {Binding}

    【讨论】:

      【解决方案2】:

      发现了一个奇怪的解决方法:

      <Window
        x:Name="Me"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="This is my text">
        <TextBlock>
          <Run Text="{Binding DataContext, ElementName=Me}"/>
        </TextBlock>
      </Window>
      

      【讨论】:

        猜你喜欢
        • 2013-03-26
        • 2012-08-06
        • 1970-01-01
        • 2012-10-24
        • 1970-01-01
        • 1970-01-01
        • 2021-09-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多