通常操作列会有多个操作项,例如:编辑、删除等,此时DataGridTextColumn 就无法满足需求,需要用到 DataGridTemplateColumn,下面是代码示例。
<DataGrid Grid.Row="1" Name="dgDataVersion" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn ElementStyle="{StaticResource DataGridTextCenter}" IsReadOnly="True" Header="版本号" Width="*" Binding="{Binding VersionNo}"/>
<DataGridTextColumn ElementStyle="{StaticResource DataGridTextCenter}" IsReadOnly="True" Header="发布内容" Width="*" Binding="{Binding Content}"/>
<DataGridTextColumn ElementStyle="{StaticResource DataGridTextCenter}" IsReadOnly="True" Header="发布时间" Width="*" Binding="{Binding Path=CreateTime,StringFormat='yyyy-MM-dd HH:mm'}"/>
<DataGridTemplateColumn Header="操作" Width="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Click="BtnEdit_Click" Name="btnEdit" Height="24" Style="{StaticResource ButtonColor}" Width="60" Content="编辑内容" Margin="10,0,0,0" IsEnabled="False"/>
<Button Click="BtnPublish_Click" Name="btnPublish" Height="24" Style="{StaticResource ButtonColor}" Width="60" Content="发布" Margin="10,0,0,0" IsEnabled="False" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>