如何将具有命名空间的 XML 绑定到 WPF DataGrid?
我有一个用例,需要将 XML 文件中的数据绑定到 WPF DataGrid。我准备这个示例是为了演示我将在最终代码中做什么。
这是 Books.xml:
<?xml version="1.0" encoding="utf-8" ?>
<library>
<books>
<book id="1" name="The First Book" author="First Author">
First Book Content
</book>
<book id="2" name="The Second Book" author="Second Author">
Second Book Content
</book>
</books>
</library>
以下是我如何将其绑定到我的 DataGrid 控件。第一个 XAML:
<Window x:Class="LinqToXmlBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="Window1" Height="300" Width="400">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="268*" />
<ColumnDefinition Width="110*" />
</Grid.ColumnDefinitions>
<toolkit:DataGrid Name="xmlBoundDataGrid" Margin="1" ItemsSource="{Binding Path=Elements[book]}">
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Header="Book ID" Binding="{Binding Path=Attribute[id].Value}"/>
<toolkit:DataGridTextColumn Header="Book Name" Binding="{Binding Path=Attribute[name].Value}"/>
<toolkit:DataGridTextColumn Header="Content" Binding="{Binding Path=Value}"/>
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
<StackPanel Name="myStackPanel" Grid.Column="1">
<Button Name="bindToXmlButton" Click="bindToXmlButton_Click">Bind To XML</Button>
</StackPanel>
</Grid>
</Window>
然后是 C# 代码:
const string _xmlFilePath = "..//..//Books.xml";
private void bindToXmlButton_Click(object sender, RoutedEventArgs e)
{
XElement books = XElement.Load(_xmlFilePath).Element(myNameSpace + "books");
xmlBoundDataGrid.DataContext = books;
}
现在,如果我在 Books.XML 的根元素处定义了一个 XML 命名空间为 http://my.namespace.com/books
;我知道我可以像这样以编程方式获取该命名空间:
XNamespace myNameSpace = XElement.Load(_xmlFilePath).Attribute("xmlns").Value;
但是,如何在 XAML 中检索此命名空间以便访问“book”元素?这方面的最佳实践是什么?
非常感谢。
I have a use-case where I need to bind data from an XML file to a WPF DataGrid. I prepared this example to demonstrate what I'll be doing in my final code.
This is Books.xml:
<?xml version="1.0" encoding="utf-8" ?>
<library>
<books>
<book id="1" name="The First Book" author="First Author">
First Book Content
</book>
<book id="2" name="The Second Book" author="Second Author">
Second Book Content
</book>
</books>
</library>
And here is how I bind it my DataGrid control. First XAML:
<Window x:Class="LinqToXmlBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="Window1" Height="300" Width="400">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="268*" />
<ColumnDefinition Width="110*" />
</Grid.ColumnDefinitions>
<toolkit:DataGrid Name="xmlBoundDataGrid" Margin="1" ItemsSource="{Binding Path=Elements[book]}">
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Header="Book ID" Binding="{Binding Path=Attribute[id].Value}"/>
<toolkit:DataGridTextColumn Header="Book Name" Binding="{Binding Path=Attribute[name].Value}"/>
<toolkit:DataGridTextColumn Header="Content" Binding="{Binding Path=Value}"/>
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
<StackPanel Name="myStackPanel" Grid.Column="1">
<Button Name="bindToXmlButton" Click="bindToXmlButton_Click">Bind To XML</Button>
</StackPanel>
</Grid>
</Window>
Then, C# code:
const string _xmlFilePath = "..//..//Books.xml";
private void bindToXmlButton_Click(object sender, RoutedEventArgs e)
{
XElement books = XElement.Load(_xmlFilePath).Element(myNameSpace + "books");
xmlBoundDataGrid.DataContext = books;
}
Now, if I defined an XML namespace at the root element of Books.XML to be http://my.namespace.com/books
; I know I can get that namespace programmatically like so:
XNamespace myNameSpace = XElement.Load(_xmlFilePath).Attribute("xmlns").Value;
But, how can I retrieve this namespace in XAML in order to access the "book" element? And what are the best practices in that regard?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,如果我弄错了,但是
如果您需要从默认命名空间访问元素,例如 xmlns="...",您应该使用常规语法,例如 Path=Attribute[name].Value
如果您的 XML 具有带前缀的命名空间,例如 xmlns:ns="... " 以及此命名空间内的元素,例如 ,您可以尝试使用 Path=Elements["ns:book"]
希望这会有所帮助。
Sorry if I got you wrong, but
If you need to access elements from default namespace like xmlns="...", you should use regular syntax like Path=Attribute[name].Value
If you have XML with namespace with prefix, like xmlns:ns="..." and elements inside this namespace like , you may try to use Path=Elements["ns:book"]
Hope this helps.