English 中文(简体)
XAML 从 XML 源的 XML 代码后面的代码插入数据
原标题:XAML DataBinding from code behind from XML source

在这里,这是我的XML

<?xml version="1.0" encoding="utf-8"?>
<app>
<films>
    <film name="Haha" year="2008" />
    <film name="Haha" year="2008" />
    <film name="Haha" year="2008" />
    <film name="Haha" year="2008" />
    <film name="Haha" year="2008" />
    <film name="Haha" year="2008" />
</films>
</app>

这里是我的XAML

<ListBox x:Name="listBoxControl">
<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel>
            <StackPanel>
                <TextBlock Text="{Binding Path=@name}" />
                <TextBlock Text="{Binding Path=@year}" />
            </StackPanel>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

在这里,这是我的C##

XDocument xmldoc = XDocument.Load(new StringReader(result));
listBoxControl.ItemsSource = xmldoc.Descendants("film");

在过去的几个小时里,我搜索了互联网和Stack 溢出问题,希望找到一个解决方案。我正在做的是,从我的网站上不同步地下载一些 XML 数据,然后将其传给ListBox 控制系统,称为“ 列表Box 控制 ” 。 问题在于“ 文本” 字段中没有任何文本。 我使用“ Path” 在装订内, 因为 XPath 不允许, 我理解这个错误 : < code> XPath 属性没有在类型“ 绑定

现在,我在这里做错什么了?这是C#的WP71应用程序,使用视像工作室快递,用于Windows 8消费者预览上运行的Windows电话。

最佳回答

我只是做一个类 和绑紧这一点:

public class Film
{
    public string name { get; set; }
    public string year { get; set; }
}

var d = xmldoc.Descendants("film").Select(x => new Film { name = x.Attribute("name").Value, year = x.Attribute("year").Value });

<TextBlock Text="{Binding name}" />
<TextBlock Text="{Binding year}" />

编辑:或匿名类型:

var d = xmldoc.Descendants("film").Select(x => new { name = x.Attribute("name").Value, year = x.Attribute("year").Value });

<TextBlock Text="{Binding name}" />
<TextBlock Text="{Binding year}" />
问题回答

在 Windows 电话上不支持使用 XPath 绑定 。

因此,唯一的解决方法就是解除 xml 和对.NET 对象的绑定。

如果您要允许用户更改数据或更新您执行的源数据,请确定您是否允许用户修改数据或更新您执行的源数据 INotify Property Changed on the class, 以便UI得到关于对象变化的通知。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签