将许多XML文件绑定到Datagrid的行

时间:2012-01-28 15:51:52

标签: wpf datagrid rows

我有30多个大小为20-30 kb的大型XML文件,其中一些信息必须作为一个数据源显示在DataGrid中。如何将1(或2或3个文件)文件绑定到DataGrid的1行?

例如,这部分XML文件:

<NameId>4366527</NameId>
<TargetType>2</TargetType>
<TargetCost>21</TargetCost>
<Tag>11</Tag>
<SupplyingRate>100</SupplyingRate>
<SupplyType>0</SupplyType>
<Transparency>0</Transparency>
<Passability>100</Passability>
<HumanPassability>100</HumanPassability>

另一个档案的这一部分:

<NameId>6591314</NameId>
<TargetType>2</TargetType>
<TargetCost>26</TargetCost>
<Tag>11</Tag>
<SupplyingRate>100</SupplyingRate>
<SupplyType>0</SupplyType>
<Transparency>0</Transparency>
<Passability>100</Passability>
<HumanPassability>100</HumanPassability>

必须显示在一个Datagrid中:

<dg:DataGrid Grid.Row="1" ItemsSource="{Binding Source={StaticResource Orders}}"
         AutoGenerateColumns="True" RowEditEnding="DataGrid_RowEditEnding">
     <dg:DataGrid.Columns>
         <dg:DataGridTextColumn Header="NameId" />
         <dg:DataGridTextColumn Header="TargetType" />
         <dg:DataGridTextColumn Header="TargetCost" />
         <dg:DataGridTextColumn Header="Tag" />
         <dg:DataGridTextColumn Header="SupplyingRate" />
         <dg:DataGridTextColumn Header="SupplyType" />
         <dg:DataGridTextColumn Header="Transparency" />
         <dg:DataGridTextColumn Header="Passability" />
         <dg:DataGridTextColumn Header="HumanPassability" />
     </dg:DataGrid.Columns>
</dg:DataGrid>

1 个答案:

答案 0 :(得分:0)

您要做的第一件事是创建一个包含XML数据的类,如下所示:

public class MyData
{
    public int NameId { get; set; }
    public int TargetType { get; set; }
    public int TargetCost { get; set; }
    public int Tag { get; set; }
    public int SupplyingRate { get; set; }
    public int SupplyType { get; set; }
    public int Transparency { get; set; }
    public int Passability { get; set; }
    public int HumanPassability { get; set; }
}

然后,您需要为DataGrid创建ViewModel,例如:

public ObservableCollection<MyData> MyList { get; set; }

public ViewModel()
{
    MyList = new ObservableCollection<MyData>();

    MyData data = new MyData()
    {
        NameId = 4366527,
        TargetType = 2,
        TargetCost = 21,
        Tag = 11,
        SupplyingRate = 100,
        SupplyType = 0,
        Transparency = 0,
        Passability = 100,
        HumanPassability = 100
    };
    MyList.Add(data);

    data = new MyData()
    {
        NameId = 6591314,
        TargetType = 2,
        TargetCost = 26,
        Tag = 11,
        SupplyingRate = 100,
        SupplyType = 0,
        Transparency = 0,
        Passability = 100,
        HumanPassability = 100
    };
    MyList.Add(data);
}

MyList是一个集合,其中包含您从XML中读取的所有数据。显然,在这个例子中,我用数据样本填充它。您必须提供填写代码。

之后,您必须创建DataGrid。我把它放在Windows中,代码是:

<Window x:Class="Temp.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"
        xmlns:local="clr-namespace:Temp">
    <Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>
    <Grid>
        <DataGrid ItemsSource="{Binding Path=MyList}"
                  AutoGenerateColumns="True">
        </DataGrid>
    </Grid>
</Window>