WPF中DataGrid解析

效果如图:

代码如下:

cs 复制代码
<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&#x0a;[mm]" Binding="{Binding ENTRY_THICKNESS}"  ElementStyle="{StaticResource MyDataGridCellStyle}"/>
        <DataGridTextColumn Width="1*"    Header="Exit Thickness&#x0a;[mm]" Binding="{Binding EXIT_THICKNESS}"  ElementStyle="{StaticResource MyDataGridCellStyle}"/>
        <DataGridTextColumn Width="1*"    Header="Exit Diam&#x0a;[mm]" Binding="{Binding COIL_DIAM}"  ElementStyle="{StaticResource MyDataGridCellStyle}"/>
        <DataGridTextColumn Width="1*"    Header="Exit Length&#x0a;[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}}}"

4、 中英文切换时DataGridTextColumn中header不能实现实时切换时:

<DataGridTextColumn Width="1*" Binding="{Binding EXIT_THICKNESS}" ElementStyle="{StaticResource MyDataGridCellStyle}">

<DataGridTextColumn.Header>

<TextBlock Text="{DynamicResource ExitThickness}" />

</DataGridTextColumn.Header>

</DataGridTextColumn>

以上即可实现实时动态绑定

相关推荐
当下就是最好11 小时前
WPF应用程序的生命周期-笔记
wpf
九鼎科技-Leo1 天前
什么是 WPF 中的依赖属性?有什么作用?
windows·c#·.net·wpf
麻花20131 天前
C#之WPF的C1FlexGrid空间的行加载事件和列事件变更处理动态加载的枚举值
开发语言·c#·wpf
lcintj1 天前
【WPF】Prism学习(九)
学习·wpf·prism
界面开发小八哥1 天前
界面控件DevExpress WPF中文教程:网格视图数据布局的列和卡片字段
wpf·界面控件·devexpress·ui开发·用户界面
△曉風殘月〆1 天前
如何在WPF中嵌入其它程序
wpf
Crazy Struggle1 天前
功能齐全的 WPF 自定义控件资源库(收藏版)
.net·wpf·ui控件库
shepherd枸杞泡茶2 天前
WPF动画
c#·.net·wpf
lcintj2 天前
【WPF】Prism学习(十)
学习·wpf·prism
wyh要好好学习2 天前
WPF数据加载时添加进度条
ui·wpf