日期:2014-05-17  浏览次数:20672 次

请教linq to xml 绑定datagrid的问题(wpf)
XML文件结构为:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <task name ="haitao" age ="22"></task>
  <task name ="zhangsan" age ="23"></task>
</root>

XAML的代码我知道如何绑定动态属性了。可是我的列是不确定的。需要在C#代码里实现

<Window x:Class="WpfApplication16.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>
        <DataGrid  AutoGenerateColumns="False" Height="150" HorizontalAlignment="Left" Margin="40,49,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="233" >
        
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Attribute[name].Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                <DataGridTextColumn Header="Age" Binding="{Binding Path=Attribute[age].Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            </DataGrid.Columns>
            </DataGrid>
    </Grid>
</Window>

横线部分。我不知道在C#中如何实现。

希望能给于指点。谢谢

 xe = XElement.Load(@"D:\C#Wpf\WpfApplication16\WpfApplication16\test.xml");
            var result = from x in xe.Elements()
                         select x;
                
            Trace.WriteLine(xDocument);
            this.dataGrid1.ItemsSource = result;
            DataGridTextColumn dgname = new DataGridTextColumn();
            Binding b  =new Binding();