本文介绍: 在DataGrid属性SelectedItem绑定单个数据源。1、给单个的列设置新的组件:在DataTemplate里设置。2、点击按钮 获取到相应数据

效果如图:

代码如下:

<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" &gt;
    <DataGrid.Columns&gt;
        <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}}}” 

原文地址:https://blog.csdn.net/Canace_Xyxjjcaw/article/details/134672975

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_4141.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注