首页 > 其他分享 >DataGrid实现Header与数据的绑定

DataGrid实现Header与数据的绑定

时间:2023-06-29 14:03:27浏览次数:42  
标签:实体类 get 绑定 DataGrid Header set new public

一.xaml代码:

       注意:列的数据可以绑定,但Hearder的绑定要使用HearderTemplate中的TextBlock元素来实现,切记。

<Window x:Class="AirtightTest.TestView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:AirtightTest"
        mc:Ignorable="d"
        Title="TestView" Height="450" Width="800">
    <Grid>
        <DataGrid x:Name="dgrid1" >
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Value1}">
                    <DataGridTextColumn.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding DataContext.ColunmTitle1, RelativeSource={RelativeSource AncestorType={x:Type local:TestView}}}"/>
                        </DataTemplate>
                    </DataGridTextColumn.HeaderTemplate>
                </DataGridTextColumn>

                <DataGridTextColumn Binding="{Binding Value2}">
                    <DataGridTextColumn.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding DataContext.ColunmTitle2, RelativeSource={RelativeSource AncestorType={x:Type local:TestView}}}"/>
                        </DataTemplate>
                    </DataGridTextColumn.HeaderTemplate>
                </DataGridTextColumn>

            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

二.定义两个实体类

// 对应表头名称的实体类
public class MyTitle { public string ColunmTitle1 { get; set; } public string ColunmTitle2 { get; set; } }
// 对应表列的数据实体类 public class MyValue { public int Value1 { get; set; } public double Value2 { get; set; } }

三.实例化对象并设置绑定上下文

// 窗体类
public partial class TestView : Window {
    // 窗体的构造中,实体化实体类并设置绑定 public TestView() { InitializeComponent(); MyTitle my = new MyTitle { ColunmTitle1 = "Title1", ColunmTitle2 = "Title2", }; DataContext = my; List<MyValue> value = new List<MyValue> { new MyValue { Value1 = 1, Value2 = 2.3 }, new MyValue { Value1 = 2, Value2 = 3.3 }, }; this.dgrid1.ItemsSource = value; } }

 

标签:实体类,get,绑定,DataGrid,Header,set,new,public
From: https://www.cnblogs.com/duju/p/17514016.html

相关文章

  • 面向对象之绑定方法,非绑定方法,隐藏属性,property装饰器,三大特征
    绑定方法#绑定给类的方法#绑定给对象的方法classStudent():school='SH'def__init__(self,name,age,gender):#self:对象自己self.name=nameself.age=ageself.gender=gender#这个就是绑定给对象的方法,有......
  • Winform DataGridView空间绘制单元格内容
    在项目中遇到一个需求,需将特定单元格内data显示为修饰后的内容,但不改变原始值(例如将data内的时间单位为ms,单元格需显示ms或者根据值显示特定名称)CellValueNeeded,该事件在控件刷新,需要为单元格填充数据时发生,其参数e返回当前单元格的行和列,根据行和列,获取需要的值,赋给e的Value属性......
  • Vscode自动生成注释的神器-KoroFileHeader
    1、安装KoroFileHeader设置-->扩展(ctrl+S)-->搜索KoroFileHeader,并安装 2、设置函数注释与文档注释设置—》搜索fileheaderconfiguration-》编辑setting.json文件设置自己的注释格式  //文件头注释"fileheader.customMade":{"Filename":"",......
  • 一图讲清楚公众号扫码关注绑定手机号自动登录
    日常开发中,相信不管做C端还是B端业务的同学都会遇到微信相关的业务,比如微信登录、微信支付、公众号扫码关注等场景。最近博主在做公众号扫码关注自动登录这一块的业务,因此总结绘制了一张公众号扫码关注绑定手机号自动登录流程图分享给大家。推荐博主开源的H5商城项目way......
  • Python高阶基础之绑定办法、非绑定方法、隐藏属性、装饰器
    绑定办法'绑定给类的办法''绑定给对象的办法'#绑定给对象classStudent:school='SH'def__init__(self,name,age,gender):self.name=nameself.age=ageself.gender=gender #这个就是绑定给对象的方法,有什么特殊之处?1.......
  • 使用 PDF一机一码加密大师,加密打包PDF文件(一机一码,绑定机器,无需额外安装阅读器)
    PDF一机一码加密大师,可以加密任意PDF文档,添加一机一码授权,静态密码等,可以禁止用户复制,打印PDF文档中的内容,并且加密生成的PDF在其他用户电脑上无需安装第三方阅读器即可直接阅读。 下载地址:点击进入下载页面 特性 1.支持一机一码加密,可以防止PDF文件未授权的分发......
  • .NET Core WEB API中参数的模型绑定方式
    .NETCoreWEBAPI中参数的模型绑定方式有以下几种:参考文献:.NETCoreWEBAPI中接口参数的模型绑定的理解-枫叶456-博客园(cnblogs.com)微软官方说明文档 FromForm:当请求属于表单提交,也就是content-type为application/x-www-form-urlencoded,则必须给参数加上From......
  • configure: error: udev support requested but libudev header not installed
    ./configure --host=arm-none-linux-gnueabi错误提示:configure:error:udevsupportrequestedbutlibudevheadernotinstalled解决办法 --disable-udev取消对libudev的依赖   ./configure --host=arm-none-linux-gnueabi --disable-udev make  ......
  • 绑定方法与非绑定方法、隐藏属性、property装饰器、面向对象的三大特征
    绑定方法绑定给对象的方法特殊之处:函数的第一个形参self就是对象自己。绑定给对象的方法就会有对象来调用,会把对象自己当成第一个参数传给第一个形参self类调用,就需要传参数,方法里面有几个参数就要传几个参数classStudent():school='SH'#类的属性def_......
  • WPF 入门笔记 - 04 - 数据绑定 - 补充内容:资源基础
    宇宙很大,生活更大,也许以后还有缘相见。--三体......