.net 绑定到xml文件的wpf DataGrid显示意外行为

.net 绑定到xml文件的wpf DataGrid显示意外行为,.net,wpf,data-binding,datagrid,wpfdatagrid,.net,Wpf,Data Binding,Datagrid,Wpfdatagrid,我有一个xml文件,看起来像: tech.xml . . 我使用xmldataprovider将其绑定到datagrid的DataGridTextColumn <XmlDataProvider x:Key="tech" Source="tech.xml" XPath="Tech" /> 像这样 <DataGrid ItemsSource="{Binding someotherclass}"> <DataGridTextColumn Binding So

我有一个xml文件,看起来像:

tech.xml

.
.
我使用xmldataprovider将其绑定到datagrid的DataGridTextColumn

<XmlDataProvider x:Key="tech" Source="tech.xml" XPath="Tech" />

像这样

<DataGrid ItemsSource="{Binding someotherclass}">

<DataGridTextColumn Binding  Source="{StaticResource tech}"   XPath="@NAME" />

.
.
.
other datagrid columns...

</DataGrid>

.
.
.
其他datagrid列。。。
我遇到的问题是DataGridTextColumn输出中的所有行都显示值“example1”

而不是人们所期望的值example1…example1333


请注意,datagrid ItemsSource被绑定到其他数据文件,只有DataGridTextColumn被绑定到tech.xml。

一种可能的方法是使用转换器。但是您需要有一些字段或属性,它们将
xml
数据与
someotherclass
数据连接起来。我创建了一个示例应用程序,它通过索引连接两个集合。希望它能有所帮助

代码如下:

MainWindow.xaml:

<Window x:Class="XmlAndSeparateSource.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:XmlAndSeparateSource"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>        
    <XmlDataProvider x:Key="TechSource" Source="tech.xml"  XPath="Tech/item" />
    <local:RowNumberConverter x:Key="rowNumberConverter" />
</Window.Resources>
<Grid>
    <DataGrid ItemsSource="{Binding }"
              AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="xml">
                <DataGridTextColumn.Binding>
                    <MultiBinding Converter="{StaticResource rowNumberConverter}">
                        <Binding Source="{StaticResource TechSource}" />
                        <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}" />
                        <Binding />
                    </MultiBinding>
                </DataGridTextColumn.Binding>
            </DataGridTextColumn>
            <DataGridTextColumn Header="string" Binding="{Binding}" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>
</Window>

MainWindow.cs

namespace XmlAndSeparateSource
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        DataContext = new List<string>()
        {
            "str1",
            "str2",
            "str3",
            "str4",
        };
        InitializeComponent();

    }
}
}
名称空间XmlAndSeparateSource
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
DataContext=新列表()
{
“str1”,
“str2”,
“str3”,
“str4”,
};
初始化组件();
}
}
}
RowNumberConverter.cs:

class RowNumberConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        //get the grid and the item
        IEnumerable<XmlNode> xml = values[0] as IEnumerable<XmlNode>;
        DataGrid grid = values[1] as DataGrid;
        Object item = values[2];
        int index = grid.Items.IndexOf(item);

        return xml.ElementAt(index).Attributes.GetNamedItem("NAME").Value;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
class RowNumberConverter:IMultiValueConverter
{
公共对象转换(对象[]值,类型targetType,对象参数,System.Globalization.CultureInfo区域性)
{
//获取网格和项目
IEnumerable xml=值[0]为IEnumerable;
DataGrid grid=值[1]为DataGrid;
对象项=值[2];
int index=grid.Items.IndexOf(item);
返回xml.ElementAt(index.Attributes.GetNamedItem(“NAME”).Value;
}
公共对象[]转换回(对象值,类型[]目标类型,对象参数,System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}
tech.xml:

<Tech>
    <item ID="1" CATEGORY="example_1" NAME="example1" MIN_LEVEL1 ="1" MAX_LEVEL1 ="10"/>
    <item ID="2" CATEGORY="example_2" NAME="example2" MIN_LEVEL1 ="1" MAX_LEVEL1 ="10"/>
    <item ID="3" CATEGORY="example_3" NAME="example3" MIN_LEVEL1 ="1" MAX_LEVEL1 ="10"/>
    <item ID="102" CATEGORY="example_1222" NAME="example1333" MIN_LEVEL1 ="1" MAX_LEVEL1 ="10"/>
</Tech>


您知道
DataGrid
将根据其
ItemsSource
属性迭代所有行吗?谢谢。这种方法有效。我确实认为datagrid应该采用更直观的方法来实现这一点。
<Tech>
    <item ID="1" CATEGORY="example_1" NAME="example1" MIN_LEVEL1 ="1" MAX_LEVEL1 ="10"/>
    <item ID="2" CATEGORY="example_2" NAME="example2" MIN_LEVEL1 ="1" MAX_LEVEL1 ="10"/>
    <item ID="3" CATEGORY="example_3" NAME="example3" MIN_LEVEL1 ="1" MAX_LEVEL1 ="10"/>
    <item ID="102" CATEGORY="example_1222" NAME="example1333" MIN_LEVEL1 ="1" MAX_LEVEL1 ="10"/>
</Tech>