【问题标题】:Binding with an anonymous object on WP7.5在 WP7.5 上与匿名对象绑定
【发布时间】:2012-12-30 01:16:46
【问题描述】:

在 WP7.5 上,我创建了这样的数据上下文:

this.DataContext = new { One = listOne, Two = listTwo};

在我的 XAML 代码中,我尝试了这个:

<TextBlock Text="{Binding listOne.m_strTypeTiers}" Style="{StaticResource PhoneTextNormalStyle}" />
<TextBlock Text="{Binding listTwo.m_strTypeTiers}" Style="{StaticResource PhoneTextNormalStyle}" />

我的文本框是空的。我认为我的绑定语法有错误。

【问题讨论】:

  • this 指的是什么?您可能还想检查 WPF 绑定警告消息的输出。
  • 我的 xamlFile 名为 page1.xaml,这是在我的 cs 页面上,名为 page1.xaml.cs。这是 Page1.xaml.cs 实例。
  • 还有一件重要的事情,如果我制作“this.datacontext = ListOne”,绑定就可以了。我的问题来自 {one = ListOne, two = ListTwo}。

标签: c# silverlight windows-phone-7 data-binding


【解决方案1】:

我想你在找{Binding One.m_strTypeTiers}

但这取决于 listOne 和 listTwo 是什么。它们是具有名为 m_strTypeTiers 的属性的类吗?如果 m_strTypeTiers 是一个字段,那么您将无法对其进行数据绑定。数据绑定仅适用于属性 (by default)。


更新
哎呀。 Silverlight does not support binding to anonymous types。我正在考虑用于桌面应用程序的 WPF。 Windows Phone 7 uses Silverlight 非常相似,但在某些重要方面有所不同。我认为你不走运 - 你需要定义一个具体的类。

您将来可能会发现这篇文章对您有所帮助:
Contrasting Silverlight and WPF(...并不是说它会帮助您解决这个特定问题)

【讨论】:

  • 这是一个类,它们是属性:)
  • 在这种情况下,您可能需要考虑阅读 Microsoft 的 Guidelines for Names
  • 感谢这个链接,不过,我尝试了 {Binding One.m_strTypeTiers} 没有成功:(
猜你喜欢
  • 2017-10-26
  • 2012-05-20
  • 1970-01-01
  • 2022-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-12
相关资源
最近更新 更多