数据网格绑定xaml
我正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
XmlDataSource
已获取所有节点/Foods/Food
,因此您无需在绑定中再次指定XPath
的该部分。尝试删除它,因此:Your
XmlDataSource
is already taking all nodes/Foods/Food
so you don't need to specify that part of theXPath
again in your bindings. Try removing it, thus:continue
I had this same problem. Add xmlns="" solved the issue for me.
You may need to add xmlns="" to Foods.
Instead of
use