数据网格绑定xaml

发布于 2024-08-17 13:07:49 字数 1700 浏览 3 评论 0原文

我正在尝试将 datagrid 绑定到 xml:

<StackPanel.DataContext>
<XmlDataProvider Source="bill.xml" XPath="/Foods/Food"/>
</StackPanel.DataContext>

<DataGrid Width="190" Height="200" HorizontalAlignment="Center" VerticalAlignment="Top" Foreground="Black" ItemsSource="{Binding}">

<DataGrid.Columns>
<DataGridTextColumn Header="Quantity" FontSize="12" Binding="{Binding XPath=/Foods/Food/Quantity}" />
<DataGridTextColumn Header="Item" FontSize="12" Binding="{Binding XPath=/Foods/Food/Item}"/>
<DataGridTextColumn Header="Price" FontSize="12" Binding="{Binding XPath=/Foods/Food/Price}"/>
<DataGridTextColumn Header="P" FontSize="12" Binding="{Binding XPath=/Foods/Food/P}"/>
<DataGridTextColumn Header="C" FontSize="12" Binding="{Binding XPath=/Foods/Food/C}"/>          
</DataGrid.Columns>

</DataGrid>

XML:

<?xml version="1.0" encoding="utf-8" ?>
    <Foods>
      <Food>
        <Quantity>1</Quantity>
        <Item>SMALL COFFEE</Item>
    <Price>0.95</Price>
    <P>N</P>
    <C>1</C>
      </Food>
    <Food>
        <Quantity>1</Quantity>
        <Item>CHEESE CAKE</Item>
    <Price>4.50</Price>
    <P>N</P>
    <C>1</C>
      </Food>
    <Food>
        <Quantity>1</Quantity>
        <Item>SPAGHETTI</Item>
    <Price>13.50</Price>
    <P>N</P>
    <C>1</C>
      </Food>

    </Foods>

DataGrid 不显示任何内容。

我错过了什么吗?

I'm trying to bind datagrid to xml:

<StackPanel.DataContext>
<XmlDataProvider Source="bill.xml" XPath="/Foods/Food"/>
</StackPanel.DataContext>

<DataGrid Width="190" Height="200" HorizontalAlignment="Center" VerticalAlignment="Top" Foreground="Black" ItemsSource="{Binding}">

<DataGrid.Columns>
<DataGridTextColumn Header="Quantity" FontSize="12" Binding="{Binding XPath=/Foods/Food/Quantity}" />
<DataGridTextColumn Header="Item" FontSize="12" Binding="{Binding XPath=/Foods/Food/Item}"/>
<DataGridTextColumn Header="Price" FontSize="12" Binding="{Binding XPath=/Foods/Food/Price}"/>
<DataGridTextColumn Header="P" FontSize="12" Binding="{Binding XPath=/Foods/Food/P}"/>
<DataGridTextColumn Header="C" FontSize="12" Binding="{Binding XPath=/Foods/Food/C}"/>          
</DataGrid.Columns>

</DataGrid>

XML:

<?xml version="1.0" encoding="utf-8" ?>
    <Foods>
      <Food>
        <Quantity>1</Quantity>
        <Item>SMALL COFFEE</Item>
    <Price>0.95</Price>
    <P>N</P>
    <C>1</C>
      </Food>
    <Food>
        <Quantity>1</Quantity>
        <Item>CHEESE CAKE</Item>
    <Price>4.50</Price>
    <P>N</P>
    <C>1</C>
      </Food>
    <Food>
        <Quantity>1</Quantity>
        <Item>SPAGHETTI</Item>
    <Price>13.50</Price>
    <P>N</P>
    <C>1</C>
      </Food>

    </Foods>

DataGrid show nothing.

Did I miss something?

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

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

发布评论

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

评论(2

以往的大感动 2024-08-24 13:07:49

您的 XmlDataSource 已获取所有节点 /Foods/Food,因此您无需在绑定中再次指定 XPath 的该部分。尝试删除它,因此:

<DataGridTextColumn Header="Quantity" FontSize="12" Binding="{Binding XPath=Quantity}" />

Your XmlDataSource is already taking all nodes /Foods/Food so you don't need to specify that part of the XPath again in your bindings. Try removing it, thus:

<DataGridTextColumn Header="Quantity" FontSize="12" Binding="{Binding XPath=Quantity}" />
又爬满兰若 2024-08-24 13:07:49

continue

I had this same problem. Add xmlns="" solved the issue for me.

You may need to add xmlns="" to Foods.

Instead of

use

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