XML 绑定到 WPF 中的 DataGrid

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27179373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:44:35  来源:igfitidea点击:

XML binding to DataGrid in WPF

c#xmlwpfdatagridvisual-studio-2013

提问by Halil Kaskavalci

I'm trying to bind XML data to DataView. I'm following this guidehowever even though I can see the result under Visual Studio 2013, I cannot see the result when I run the program. I tried adding Movies.xml to Resources and adding it to project directory. Currently it resides under project root path. While I was writing this post, for a while I saw the result while project is running, however after clean build it disappeared again.

我正在尝试将 XML 数据绑定到 DataView。我正在遵循本指南,但是即使我可以在 Visual Studio 2013 下看到结果,我在运行程序时也看不到结果。我尝试将 Movies.xml 添加到 Resources 并将其添加到项目目录。目前它位于项目根路径下。在我写这篇文章的时候,有一段时间我在项目运行时看到了结果,但是在干净构建之后它又消失了。

Movies.XML:

电影.XML:

<?xml version="1.0" encoding="utf-8" ?>
<Movies xmlns="">
  <Movie Name="Seven Samurai" Id="101" Director="Akira Kurosawa" />
  <Movie Name="Happy Together" Id="102" Director="Wong Kar Wai"/>
  <Movie Name="Shoot The Piano Player" Id="103" Director="Francois Truffaut"/>
  <Movie Name="Roshomon" Id="104" Director="Akira Kurosawa" />
  <Movie Name="Dead Man" Id="105" Director="Jim Jarmusch"/>
  <Movie Name="Children of Heaven" Id="106" Director="Majid Majidi"/>
</Movies>

XAML:

XAML:

<Window x:Class="DataGridXML.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <XmlDataProvider x:Key="MovieData" Source="Movies.xml" XPath="/Movies/Movie"/>
        </Grid.Resources>
        <DataGrid x:Name="dgridEmp" DataContext="{StaticResource MovieData}" ItemsSource="{Binding XPath=/Movies/Movie}"
 AutoGenerateColumns="False" Margin="52,89,31,50">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding XPath=@Name}" />
                <DataGridTextColumn Header="Title" Binding="{Binding XPath=@Director}" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

Visual Studio Preview: Visual Studio Preview

视觉工作室预览: Visual Studio 预览版

Project run-time:

项目运行时间:

Run time

运行

My final goal is to bind such XML dynamically, so that user will choose which XML to parse. Then user can change the content and save.

我的最终目标是动态绑定此类 XML,以便用户选择要解析的 XML。然后用户可以更改内容并保存。

回答by Umut Koseali

You can do it like this, first convert your xml to a DataSetthen simply set your DataGrid's ItemsSourceto DataView:

您可以这样做,首先将您的 xml 转换为 aDataSet然后只需将您的DataGrid's设置ItemsSourceDataView

string sampleXmlFile = @"C:\Users\umut.koseali\Desktop\test.xml";
DataSet dataSet = new DataSet();
dataSet.ReadXml(sampleXmlFile);
DataView dataView = new DataView(dataSet.Tables[0]);
dataGrid1.ItemsSource = dataView

XAML:

XAML:

<DataGrid x:Name="dataGrid1" />

回答by Medeni Baykal

Are you sure, that you marked the "Movies.xml" as Content in the IDE?

您确定在 IDE 中将“Movies.xml”标记为“内容”吗?

Sample

样本

You could read more from MSDN.

您可以从MSDN阅读更多内容。