效果如图:
代码如下:
<DataGrid Grid.Row="1" x:Name="dataGrid" ItemsSource="{Binding DataList}" AutoGenerateColumns="False"SelectedItem="{Binding SelectedItem,UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True"ColumnHeaderStyle="{StaticResource UnitColumnHeader}" RowHeaderWidth="0" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ><DataGrid.Columns><DataGridTextColumn Width="1.5*" Header="CoilId" Binding="{Binding COIL_ID}" ElementStyle="{StaticResource MyDataGridCellStyle}"/><DataGridTextColumn Width="1.2*" Header="AlloyId" Binding="{Binding ALLOY_ID}" ElementStyle="{StaticResource MyDataGridCellStyle}"/><DataGridTextColumn Width="1*" Header="Entry Thickness
[mm]" Binding="{Binding ENTRY_THICKNESS}" ElementStyle="{StaticResource MyDataGridCellStyle}"/><DataGridTextColumn Width="1*" Header="Exit Thickness
[mm]" Binding="{Binding EXIT_THICKNESS}" ElementStyle="{StaticResource MyDataGridCellStyle}"/><DataGridTextColumn Width="1*" Header="Exit Diam
[mm]" Binding="{Binding COIL_DIAM}" ElementStyle="{StaticResource MyDataGridCellStyle}"/><DataGridTextColumn Width="1*" Header="Exit Length
[mm]" Binding="{Binding COIL_LENGTH}" ElementStyle="{StaticResource MyDataGridCellStyle}"/><DataGridTextColumn Width="1.2*" Header="StartTime" Binding="{Binding START_TIME}" ElementStyle="{StaticResource MyDataGridCellStyle}"/><DataGridTextColumn Width="1.2*" Header="EndTime" Binding="{Binding STOP_TIME}" ElementStyle="{StaticResource MyDataGridCellStyle}"/><DataGridTextColumn Width="1.2*" Header="TotalTime" Binding="{Binding DURATIONTIME}" ElementStyle="{StaticResource MyDataGridCellStyle}"/><DataGridTemplateColumn Width="1.2*" Header="PDF" ><DataGridTemplateColumn.CellTemplate><DataTemplate><Button Content="{Binding COIL_ID}" Width="196" Style="{StaticResource BasicButtonStyle}" Command="{Binding DataContext.MakePDFReport,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" /></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn></DataGrid.Columns>
</DataGrid>
1、给单个的列设置新的组件:在DataTemplate里设置
<DataGridTemplateColumn Width="1.2*" Header="PDF" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="{Binding COIL_ID}" Width="196" Style="{StaticResource BasicButtonStyle}" Command="{Binding DataContext.MakePDFReport,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
2、点击按钮 获取到相应数据
在DataGrid的属性SelectedItem绑定单个数据源
SelectedItem="{Binding SelectedItem,UpdateSourceTrigger=PropertyChanged}"
3、DataGrid里的事件绑定特殊:
Command="{Binding DataContext.MakePDFReport,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"