数据绑定 ObservableCollection到列表框?

发布于 2024-10-07 08:46:12 字数 1248 浏览 0 评论 0原文

我正在开发一个 Silverlight 应用程序,并且想要将 ListBoxItemsSource 设置为 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 技术交流群。

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

发布评论

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

评论(2

柳若烟 2024-10-14 08:46:12

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 those Element and other pseudoproperties are exposed to databinding. This attribute is not present in the Silverlight version of XElement. (And it can't be present because Silverlight doesn't provide a definition of TypeDescriptionProvider, 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.

冷血 2024-10-14 08:46:12

使用 XPath 代替:

<TextBlock Text="{Binding XPath=<..YourXPathSyntax..>}" />

编辑:

正如 Ian 指出的,您正在使用 Silverlight。因此,XPath 将不起作用。为什么不使用 IValueConverter 来代替呢?

例子 :

using System.Xml.Linq;
using System.Xml.XPath;

public class IXPathConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        String xpath = (String)parameter;
        XElement element = (XElement)value;

        return element.Document.XPathSelectElement(xpath);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Use XPath instead :

<TextBlock Text="{Binding XPath=<..YourXPathSyntax..>}" />

EDIT:

As Ian pointed out, you are using Silverlight. So, XPath will not work. Why not use IValueConverter instead?

Example :

using System.Xml.Linq;
using System.Xml.XPath;

public class IXPathConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        String xpath = (String)parameter;
        XElement element = (XElement)value;

        return element.Document.XPathSelectElement(xpath);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文