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
勇敢小菜鸟21 小时前
WPF自定义窗口 输入验证不生效
wpf
鲤籽鲲21 小时前
WPF TextBox 输入限制 详解
wpf
鸿喵小仙女21 小时前
C# WPF读写STM32/GD32单片机Flash数据
stm32·单片机·c#·wpf
六点的晨曦21 小时前
WPF的右键菜单项目引入DLL和DllImport特性引入DLL文件的异同点
wpf
一个不正经的林Sir1 天前
C#WPF基础介绍/第一个WPF程序
开发语言·c#·wpf
可喜~可乐1 天前
C# WPF开发
microsoft·c#·wpf
界面开发小八哥1 天前
DevExpress WPF中文教程:Grid - 如何移动和调整列大小?(二)
ui·.net·wpf·界面控件·devexpress·ui开发
界面开发小八哥2 天前
「实战应用」如何用图表控件SciChart WPF实现应用程序的DPI感知?
信息可视化·wpf·数据可视化·图表·scichart wpf·scichart
明耀3 天前
WPF TabControl 设置item不能点击
wpf