出现绑定错误但没有绑定集?

发布于 2024-12-11 23:24:10 字数 663 浏览 0 评论 0原文

我的 WPF 窗口中有这个控件;

<TextBlock Name="txtMyTextBlock"
          Text="{x:Static Resources:Strings.SOME_LABEL_IDENTIFIER}"
          Margin="10,10,10,0" HorizontalAlignment="Left" VerticalAlignment="Top"
          Style="{StaticResource WindowTitleStyle}" />

当加载窗口时,我在输出窗口中收到此错误;

Windows.Data 错误:40:BindingExpression 路径错误:“文本”属性 在“对象”“MyViewModelClass”(HashCode=7754709)”上找不到。 绑定表达式:路径=文本; DataItem='MyViewModelClass' (哈希码=7754709);目标元素是“TextBlock” (名称='txtMyTextBlock');目标属性为“NoTarget”(类型“Object”)

据我所知,TextBlock“txtMyTextBlock”上的“Text”属性设置为嵌入式资源字符串...为什么我会在这里收到绑定错误?

I have this control in my WPF window;

<TextBlock Name="txtMyTextBlock"
          Text="{x:Static Resources:Strings.SOME_LABEL_IDENTIFIER}"
          Margin="10,10,10,0" HorizontalAlignment="Left" VerticalAlignment="Top"
          Style="{StaticResource WindowTitleStyle}" />

and when the window is loaded, I get this error in the Output Window;

Windows.Data Error: 40 : BindingExpression path error: 'Text' property
not found on 'object' ''MyViewModelClass' (HashCode=7754709)'.
BindingExpression:Path=Text; DataItem='MyViewModelClass'
(HashCode=7754709); target element is 'TextBlock'
(Name='txtMyTextBlock'); target property is 'NoTarget' (type 'Object')

As far as I can tell, the 'Text' property on the TextBlock 'txtMyTextBlock' is set to an Embedded Resource string... why am I getting binding errors here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

演多会厌 2024-12-18 23:24:10

在您的 Text="" 中,您绑定到静态资源。这是可以做到的,但你的语法是错误的。你想要的是这样的:

<Window.Resources>
   <System:String x:Key="FirstName">Jared</System:String>
</Window.Resources>

<TextBox Text="{StaticResource FirstName}"/>

in your Text="" you are binding to a static resource. this can be done, but your syntax is wrong. what you want is something like this:

<Window.Resources>
   <System:String x:Key="FirstName">Jared</System:String>
</Window.Resources>

<TextBox Text="{StaticResource FirstName}"/>

here is a good walk-though/tutorial

來不及說愛妳 2024-12-18 23:24:10

问题不应该出现在这段代码中,x:Static 不会导致绑定错误,我怀疑您应用的样式中存在失败的绑定,例如

<Style x:Key="WinTitleStyle" TargetType="TextBlock">
    <Setter Property="Text" Value="{Binding ThisFails}" />
</Style>
<!-- ... -->
<TextBlock Name="myTB" Text="{x:Static prop:Resources.WinTitle}"
           Style="{StaticResource WinTitleStyle}"/>

即使样式设置器没有优先级 我从中收到错误绑定:

System.Windows.Data 错误:40:BindingExpression 路径错误:
在“对象”“MainWindow”上找不到“ThisFails”属性
(名称='窗口')'。 BindingExpression:Path=ThisFails;
DataItem='主窗口'(名称='窗口');目标元素是“TextBlock”
(名称='myTB');目标属性是“文本”(类型“字符串”)

The problem should not be in this bit of code, x:Static does not cause binding errors, i suspect that there is a failing binding in the style you applied, e.g.

<Style x:Key="WinTitleStyle" TargetType="TextBlock">
    <Setter Property="Text" Value="{Binding ThisFails}" />
</Style>
<!-- ... -->
<TextBlock Name="myTB" Text="{x:Static prop:Resources.WinTitle}"
           Style="{StaticResource WinTitleStyle}"/>

Even though the style setter does not have precedence i get an error from that binding:

System.Windows.Data Error: 40 : BindingExpression path error:
'ThisFails' property not found on 'object' ''MainWindow'
(Name='Window')'. BindingExpression:Path=ThisFails;
DataItem='MainWindow' (Name='Window'); target element is 'TextBlock'
(Name='myTB'); target property is 'Text' (type 'String')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文