XML文件
<Tree>
<Item>
<编号>编号</编号>
<树冠>树冠半径</树冠>
<树干>树干直径</树干>
<树龄>树龄</树龄>
<古树等级>古树等级</古树等级>
</Item>
<Item>
<编号>T1</编号>
<树冠>3.3m</树冠>
<树干>22cm</树干>
<树龄 />
<古树等级 />
</Item>
<Item>
<编号>T2</编号>
<树冠>2.5m</树冠>
<树干>20cm</树干>
<树龄 />
<古树等级 />
</Item>
</Tree>
前台
<DataGrid Name="dgTree" ItemsSource="{Binding}" AlternationCount="2" BorderThickness="0" Background="#000" VerticalAlignment="Top" HorizontalScrollBarVisibility="Hidden" HeadersVisibility="None" AutoGenerateColumns="False" CanUserReorderColumns="False" HorizontalAlignment="Center" GridLinesVisibility="None" >
<DataGrid.Columns>
<DataGridTextColumn Width="70" Binding="{Binding 编号}" ElementStyle="{StaticResource dgCellTree}" />
<DataGridTextColumn Width="70" Binding="{Binding 树冠}" ElementStyle="{StaticResource dgCellTree}" />
<DataGridTextColumn Width="70" Binding="{Binding 树干}" ElementStyle="{StaticResource dgCellTree}"/>
<DataGridTextColumn Width="70" Binding="{Binding 树龄}" ElementStyle="{StaticResource dgCellTree}"/>
<DataGridTextColumn Width="70" Binding="{Binding 古树等级}" ElementStyle="{StaticResource dgCellTree}"/>
</DataGrid.Columns>
<!--表格隔行显示不同颜色-->
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="#717171" />
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#000" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Resources>
<!--选中表格后,鼠标离开后表格背景色-->
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#000"/>
</DataGrid.Resources>
</DataGrid>
后台读取XML
string filePath = System.Windows.Forms.Application.StartupPath + "/Resource/XML/" + tvData + "/" + tvData + ".xml";
if (!File.Exists(filePath))
{
return;
}
else
{
//获取XML中的数据,返回给DataGrid
System.Data.DataSet ds = new System.Data.DataSet();
ds.ReadXml(filePath);
System.Data.DataTable dt = ds.Tables[0];
mainWindow.dgTree.ItemsSource = dt.DefaultView;
}
dgCellTree设置表格样式
<Window.Resources>
<ResourceDictionary>
<Style x:Key="dgCellRight" TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Right" />
<Setter Property ="Foreground" Value="#C0C0C0"/>
<Setter Property ="Height" Value="20"/>
<Setter Property ="Margin" Value="0,10,0,0"/>
</Style>
<Style x:Key="dgCellLeft" TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Left"/>
<Setter Property ="Foreground" Value="#FFFFFF"/>
<Setter Property ="Margin" Value="20,10,0,0"/>
</Style>
<Style x:Key="dgCellTree" TargetType="TextBlock" >
<Setter Property ="Height" Value="25"/>
<Setter Property ="Foreground" Value="#FFFFFF"/>
<Setter Property ="FontSize" Value="14"/>
<Setter Property ="VerticalAlignment" Value="Center"/>
<Setter Property ="HorizontalAlignment" Value="Center"/>
<Setter Property ="Margin" Value="0,5,0,0"/>
</Style>
<Style x:Key="dgCell" TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Center" />
<Setter Property ="Foreground" Value="#fff"/>
<Setter Property ="Height" Value="30"/>
<Setter Property ="Margin" Value="0,10,0,0"/>
<Setter Property ="FontSize" Value="16"/>
</Style>
</ResourceDictionary>
</Window.Resources>