wpf - 无法将绑定(bind)设置为驻留在 TextBlock 中的运行

标签 wpf binding textblock bindable

<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 :“双向绑定(bind)需要 Path 或 XPath。”

指定 Mode=OneWay ,导致一个奇怪的编译器错误:
The tag 'Binding,' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.
有什么方法可以解决这个问题吗?

最佳答案

我还没有找到原因,但这就是你如何做到这一点而不会变得太尴尬:

<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=.}" />

才不是。原因可能与您对绑定(bind)“模棱两可”有关,某些后备行为会接管来解释您的绑定(bind)。或者,这是一个真正的 MS 错误,带有解释 {Binding}Run控制。

关于wpf - 无法将绑定(bind)设置为驻留在 TextBlock 中的运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4481386/

相关文章:

c# - 如何清除 BindingList Comepeltly

wpf - 我怎样才能拥有一个所有字体粗细都相同的 WPF TextBlock?

XAML TextBlock TextAlignment 与 Horizo​​ntalAlignment

.net - 使 WPF 应用程序看起来像 Metro 风格,即使在 Windows 7 中也是如此? (窗口 Chrome/主题/主题)

c# - WPF 中的图像变得模糊

c# - 多级时如何将 View 绑定(bind)到 VM 包装器而不是直接绑定(bind)到模型

TextBlock : nothing happens when I click on it 中的 C# 超链接

c# - 如何在 WPF 中使用自定义控件

c# - 在 StaticResource Canvas 中为 Path 设置不同的大小或自动调整大小

binding - Ninject 绑定(bind)约束,向上搜索以查找类型