数据绑定 ObservableCollection到列表框?
我正在开发一个 Silverlight 应用程序,并且想要将 ListBox
的 ItemsSource
设置为 ObeservableCollection
并且仍然能够使用Binding Path=Element[name].Value
语法来获取数据模板的值。我可以成功绑定,但 Element[] 语法不起作用。它只是呈现空。例如,这不起作用:
<DataTemplate x:Key="SearchResultsTemplate">
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Element[key].Value}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
但奇怪的是,类似这样的东西确实会渲染内容,这告诉我一切都在某种程度上绑定,但有些东西阻止我使用 Element 动态属性:
<DataTemplate x:Key="SearchResultsTemplate">
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=FirstNode}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
我做错了什么?
I'm developing a Silverlight application, and want to set the ItemsSource
of a ListBox
to ObeservableCollection<XElement>
and still be able to use the Binding Path=Element[name].Value
syntax to get values for a data template. I can get the binding successfully, but the Element[] syntax is not working. It just renders empty. For example, this does not work:
<DataTemplate x:Key="SearchResultsTemplate">
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Element[key].Value}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
But oddly, something like this does render content, which tells me everything is bound to some degree, but something is keeping me from using the Element dynamic property:
<DataTemplate x:Key="SearchResultsTemplate">
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=FirstNode}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Element
伪属性仅在 .NET 桌面版本中可用。它依赖于类型描述符系统,该系统是 Silverlight 中缺少的 .NET Framework 的组成部分之一。在完整的 .NET 框架中,
XElement
有一个[TypeDescriptionProvider(typeof(XTypeDescriptionProvider))]
属性,这就是这些Element
和其他伪属性暴露于数据绑定。XElement
的 Silverlight 版本中不存在此属性。 (它不能存在,因为 Silverlight 不提供TypeDescriptionProvider
的定义,或该属性背后的任何关联机制。Silverlight不提供绑定到 XML 内容的直接方法。 (它也不支持 XPath,这是 WPF 中流行的另一种解决方案。)您可以查看 在 Silverlight 中绑定 XML,无需名义类,链接到 Graham Murray 的一篇文章,该文章展示了如何动态生成可绑定类型。
不过,这是一个相对复杂的解决方案。我想我只需编写一个包装类型获取包含我想要的数据的 XML,并使用 LINQ to XML 从 XML 填充这些包装器。
The
Element
pseudoproperty is only available in the desktop version of .NET. It relies on the type descriptor system which is one of the pieces of the .NET Framework that's missing in Silverlight.In the full .NET framework,
XElement
has a[TypeDescriptionProvider(typeof(XTypeDescriptionProvider<XElement>))]
attribute, which is how thoseElement
and other pseudoproperties are exposed to databinding. This attribute is not present in the Silverlight version ofXElement
. (And it can't be present because Silverlight doesn't provide a definition ofTypeDescriptionProvider
, or any of the associated machinery behind that attribute.Silverlight doesn't provide a direct way to bind to XML content. (It doesn't support XPath either, which is the other solution popular in WPF.) You could look at Binding XML in Silverlight without nominal classes which links to an article by Graham Murray which shows how to generate bindable types dynamically.
That's a relatively complex solution though. I think I'd just write a wrapper type for the XML that includes the data I want, and use LINQ to XML to populate those wrappers from the XML.
使用 XPath 代替:
编辑:
正如 Ian 指出的,您正在使用
Silverlight
。因此,XPath
将不起作用。为什么不使用IValueConverter
来代替呢?例子 :
Use XPath instead :
EDIT:
As Ian pointed out, you are using
Silverlight
. So,XPath
will not work. Why not useIValueConverter
instead?Example :