C#:WPF datagrid和xml文件 [英] C#: WPF datagrid and xml file

查看:108
本文介绍了C#:WPF datagrid和xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用类读取xml文件并将其填充到datagrid? datagrid应该具有验证功能?

How can I read a xml file using a class and populate it on a datagrid? The datagrid should have validation capabilities?

Xml文件:

<?xml version='1.0'?>
<Data>
 <Book>
 <Author>John Doe</Author>
 <Title>Straight Track Demo</Title>
 <Version>1</Version>  
 </Book>
</Data>


推荐答案

有几种方法可以加载DataGrid与XML(还有其他的):

There are a couple of ways you can load up a DataGrid with XML (there are others as well):


  1. 使用 XmlDataProvider

  2. 从代码隐藏中读取XML >
  1. Using an XmlDataProvider
  2. Reading the XML in from the code-behind

这是一个非常粗糙的样本,使用这两种方法。

Here's a very crude sample that uses both methods.

XAML

<Window x:Class="WpfApplication1.MyDataGrid"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MyDataGrid" Height="300" Width="300">
    <Window.Resources>

        <XmlDataProvider x:Key="BookData" 
            Source="C:\Somewhere\Books.xml" XPath="Data"/>

    </Window.Resources>
    <StackPanel>
        <DataGrid 
            ItemsSource="{Binding Path=Elements[Book]}"
            AutoGenerateColumns="False" Height="Auto" 
            Name="dataGrid1" 
            VerticalAlignment="Top" HorizontalAlignment="Stretch">

            <DataGrid.Columns>
                <DataGridTextColumn 
                    Header="Author" 
                    Binding="{Binding Path=Element[Author].Value}"/>
                <DataGridTextColumn 
                    Header="Title" 
                    Binding="{Binding Path=Element[Title].Value}"/>
                <DataGridTextColumn 
                    Header="Version" 
                    Binding="{Binding Path=Element[Version].Value}" />
            </DataGrid.Columns>

        </DataGrid>


        <DataGrid 
            DataContext="{StaticResource BookData}" 
            ItemsSource="{Binding XPath=Book}"
            AutoGenerateColumns="False" Height="Auto" 
            Name="dataGrid2" Margin="0,25,0,0" 
            VerticalAlignment="Top" HorizontalAlignment="Stretch">

            <DataGrid.Columns>
                <DataGridTextColumn 
                    Header="Author" 
                    Binding="{Binding XPath=Author}"/>
                <DataGridTextColumn 
                    Header="Title" 
                    Binding="{Binding XPath=Title}"/>
                <DataGridTextColumn 
                    Header="Version" 
                    Binding="{Binding XPath=Version}" />
            </DataGrid.Columns>

        </DataGrid>

    </StackPanel>
</Window>

背后的代码

Code Behind

using System.Windows;
using System.Xml.Linq;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MyDataGrid.xaml
    /// </summary>
    public partial class MyDataGrid : Window
    {
        public MyDataGrid()
        {
            InitializeComponent();

            var xml = XDocument.Load( "c:\\Somewhere\\Books.xml" ).Root;
            dataGrid1.DataContext = xml;
        }

    }
}





最后,这里有几篇文章:

For Reference

Finally, here are a couple of articles:


  1. MSDN上的Binding.XPath属性

  2. 使用数据绑定和WPF自定义数据显示>使用XML数据

  3. 使用XML数据的DataGrid示例

  1. Binding.XPath Property on MSDN
  2. Customize Data Display with Data Binding and WPF > Using XML Data
  3. A DataGrid sample using XML data

这篇关于C#:WPF datagrid和xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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