XML数据绑定与命名空间 [英] XML Data Binding with Namespaces

查看:109
本文介绍了XML数据绑定与命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用数据绑定与XML文档填充一个简单的表单,显示有关人员列表的详细信息。我现在已经完成了所有的设置和工作:

I want to use data binding with an XML document to populate a simple form that shows details about a list of people. I've got it all set up and working like so right now:

<Window x:Class="DataBindingSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1">
<Window.Resources>
    <XmlDataProvider x:Key="xmlProvider" XPath="People" Source="c:\someuri.xml"/>
</Window.Resources>
<Grid>        
    <ListBox Name="personList" ItemsSource="{Binding Source={StaticResource xmlProvider}, XPath=Person}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding XPath=Name}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    <GroupBox Header="GroupBox" Name="groupBox1" DataContext="{Binding ElementName=personList, Path=SelectedItem}">
        <Grid>
            <TextBox Name="nameText" Text="{Binding XPath=Name}"/>
            <ComboBox Name="genderCombo" Text="{Binding XPath=Gender}">
                <ComboBoxItem>Male</ComboBoxItem>
                <ComboBoxItem>Female</ComboBoxItem>
            </ComboBox>
        </Grid>
    </GroupBox>
</Grid>
</Window>

(为了清楚起见,所有的位置/布局元素已被删除)

(All position/layout elements have been removed for clarity)

现在这个很棒!如果我提供一些与所提供路径匹配的XML,我会在列表框中获取名单列表,该列表在点击时显示相应字段中的名称和性别。当我开始在我的XML源中尝试使用命名空间时,会出现问题。 XAML然后改变如下:

Now this works great! If I provide it with some XML that matches the paths provided I get a list of names in the listbox that show both the name and gender in the appropriate fields when clicked. The problem comes when I start to try and use namespaces in my XML source. The XAML then changes to look like this:

<Window x:Class="DataBindingSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1">
<Window.Resources>
    <XmlNamespaceMappingCollection x:Key="namespaceMappings">
        <XmlNamespaceMapping Uri="http://www.mynamespace.com" Prefix="mns"/>
    </XmlNamespaceMappingCollection>
    <XmlDataProvider x:Key="xmlProvider" XmlNamespaceManager="{StaticResource namespaceMappings}" XPath="mns:People" Source="c:\someuriwithnamespaces.xml"/>
</Window.Resources>
<Grid>        
    <ListBox Name="personList" ItemsSource="{Binding Source={StaticResource xmlProvider}, XPath=mns:Person}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding XPath=mns:Name}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    <GroupBox Header="GroupBox" Name="groupBox1" DataContext="{Binding ElementName=personList, Path=SelectedItem}">
        <Grid>
            <TextBox Name="nameText" Text="{Binding XPath=mns:Name}"/>
            <ComboBox Name="genderCombo" Text="{Binding XPath=mns:Gender}">
                <ComboBoxItem>Male</ComboBoxItem>
                <ComboBoxItem>Female</ComboBoxItem>
            </ComboBox>
        </Grid>
    </GroupBox>
</Grid>
</Window>

当然,使用这段代码(当然也有适当的命名空间xml),Listbox仍然正确显示名称,但点击这些名称不再更新名称和性别字段!我的怀疑是,不知何故xml命名空间对组框的DataContext产生不利影响,但我不知道为什么或如何。有谁知道如何在这个上下文中使用XML命名空间?

With this code (and the appropriately namespaced xml, of course) the Listbox still displays the names properly, but clicking on those names no longer updates the Name and Gender fields! My suspicion is that somehow the xml namespace is reacting adversely to the groupbox's DataContext, but I'm not sure why or how. Does anyone know how to use XML namespaces in this context?

推荐答案

您可以在XPath查询中使用本地名称,如下所示:

You could use local names in your XPath queries like this:

 <TextBox Name="nameText">
    <TextBox.Text>
       <Binding XPath="*[local-name()='Name']" />
    </TextBox.Text>
 </TextBox>

这篇关于XML数据绑定与命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆