WPF DataGrid绑定及列居中
基本的数据绑定
把集合的字段(属性)绑定在DataGrid的Binding属性就能将数据绑定列表
public class CashItem {
public int Value { get; set; }
public int Count { get; set; }
public int Amount { get { return Value * Count; } }
}
var items = new List<CashItem>() {
new CashItem{Value=,Count=},
new CashItem{Value=,Count=},
new CashItem{Value=,Count=},
new CashItem{Value=,Count=}
};
this.Cassetters.ItemsSource = items;
<DataGrid Name="Cassetters" HeadersVisibility="Column" GridLinesVisibility="Horizontal" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="面值" Width="80" Binding="{Binding Value}">
</DataGridTextColumn>
<DataGridTextColumn Header="张数" Width="80" Binding="{Binding Count}"></DataGridTextColumn>
<DataGridTextColumn Header="金额" Width="*" Binding="{Binding Amount}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARAAAACaCAIAAADuNIBIAAAHlUlEQVR4nO2dPY7cRhCFeZc9wEYCmOsWAjbbiLFi5QKUMlzAN1A8DJ1vvuYhZN+ADvjXTTY5/dqih6z+PjzYO720MVXF190kxVLx6+9fCKFIFQ//BghdSEWXE7fb7dFf4X8lt3iP43a7YRj75BbvcWCYLMgt3uPAMFmQW7zHgWGyILd4jwPDZEFu8R7HwYZpqqIo6/aQ//d/IbcTKLd4jyPWME1V3KFq7h54HuvcPYHaunQj84zfVMPPbV0WVdPWZTH8q2qGBAzJOA2iYYYihurV1mXhhB88ByyTssL0iQonZ3G6XH2FGcwwxzHNB1XTdW1TlUVV12VRVFVVFGXVtKf0i2QYd8rzAlnOhX1hz1rig4g3zP4a02d2mn6CB5/oNEozTO2bITDD1v7QaU4kKd7hew8/+7Pf9KmpirJcxH+iAh+GZpiybsdTKDQyLz3Lacf5r07B/RPIccNySxY6bM7IsPicK+AYw4yhOHH60XX+HNFPIOPRnpsMk2YYbw6dzo1hWXF+PO0EFGcY92QIRdMf0ywXmtCM8WDuxhtzjdqXvqzbMTnzutv/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U8dxmtMA679c0lCRrRhrHSs2+DiMaFBtmtby5J0Fgbpqmie7oZIqZxoQ3i62s4CemEVpi8/hYdpXGhDeLrazgJqfCkH0AAwwAIYBgAAQwDIIBhAAQwDIAAhgEQoJEfQoJYYQAEMAyAAIYBEMAwAAIYBkAAwwAIYBgAAQwDIBAyjPNKlf9St82ebhuN7UwT27jQbNGTCb2iPCVnfofVck+3zF6/jW9caLnoyexvydq6XPfhsdYPITPD9EQ0LjRd9GTudo2pms5426FAYzv7RLSVMl30ZPYMM+Uok55uzhbEPPcbF2ZSdJVNw3i71lwmG7uRLWGFSSRsmOU1Xi493fI5KyIaF+ZSdI21YYIbE8M93YKN7cwT07jQcNHTWRnGeyrh3IA33NMtr4cNSuNCw0VPhSf9AAIYBkAAwwAIYBgAAQwDIIBhAAQwDIAAjfwQEjQY5o+PfxBCd4VhEBKEYRAShGEQEoRhEBKEYRAShGEQEoRhEBK0YZi316IoPr+5gz8/D+8SfXq5Pf57/2YF4t0YtKHYeE0XPUlrw7y/PBfF8+vnZzd37y/PxdO39zGtr18f/b1/nzbjXQ3aUHy8houerq0t2fuLm7vbj6c5X/6vjCgYlMlIo+O1X/QUxRnm7bX48nP67dcv48RjRxhmNWi/6CmKMsz3b5+s5w7DLAczKHqKWGFC8e4N2hArTKKiDfP84/terq8uDLMatF/0FMUZxv3o5dGMMMx60HzRU7RxW9lhSNntx9MwYOz2YjDejSRYkBKv2aKniyf9CAnCMAgJwjAICcIwCAnCMAgJwjAICcIwCAmikR9CgmgVCyCAYQAEMAyAAIYBEMAwAAIYBkAAwwAIYBgAAQwDIIBhAAQwDIAAhgEQwDAAAhgGQADDAAhgGAABDAMggGEABDAMgACGARDAMAACGAZAAMMACGAYAAEa+SEkqDfMv/BMn7qom/MGAAAAAElFTkSuQmCC" alt="" width="300" />
DataGrid列居中
列居中需要使用到了模板,但是列头的居中需要自己定义样式,数据的绑定方式和之前的一样也是通过Binding属性
<DataGrid Name="Cassetters" HeadersVisibility="Column" GridLinesVisibility="Horizontal" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="面值" Width="50" Binding="{Binding Value}">
</DataGridTextColumn>
<DataGridTextColumn Header="张数" Width="50" Binding="{Binding Count}"></DataGridTextColumn>
<DataGridTextColumn Header="金额" Width="50" Binding="{Binding Amount}"></DataGridTextColumn>
<DataGridTemplateColumn Header="模板列" Width="*">
<DataGridTemplateColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
</Style>
</DataGridTemplateColumn.HeaderStyle>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" Text="{Binding Amount}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQ4AAACZCAIAAABRx0O9AAAM5UlEQVR4nO2dz2sc5xnH3z/APpTo0EIuiUmji1EvHk17KyqlFGzIJQJXgh7ErjyHnOxbYkKLD1YNmjUaKBSDLyY9GKRKq5Kd3lpMILCHlWQj7yG2fq0dE7KSYq1kg6Me5p2Z9515d/cZWTvvu5vvh4dm953ZnWefeT/zzo/qNfu++T0CgegaTHsGCERfBDs2j0qlojsFBcZmtb+/rzuLwWd/fx+qUDE2K6iSA1AlA8Zmtbe3pzuLwWdvbw+qUDE2K6iSA1AlA8ZmBVVyAKpkwNisdnd3dWcx+Ozu7vZGFd9hzPbqJ/y0sZ1SdwoKoEo+dFfFd1gXHL/rilml6dop654tbl8S03f467pnM8evezbj/3F8niZPOSMZVeEFUf32umczIUllPTNkBVVyINuoEuxW9a5MdMF8RhWuQby1yFfHPz6u+47NHM+zGXMchzHb8etvYUomVcQDh7S55BElKNJblMsIVeqeHafvO9FRQPpNYXv772h/jK179gn32mlBUaXzuBL8gOgwqVw58488mSqerIHieO3JTRk7Z6as+Lfz1/IxJHrnO8y2E1lmLVal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QAgkVLFdxhjjp9qYowx26vnl1m3rNo05gk1KwMKqIfazDBjF+8979ZYmxkOCvThp1/t55siHVGVumczZjuOLe7pumeHe9h3dPTMtlmlGs3MSnsB9dBYmBhiH/7J+eOQYEXbRq5IbWY4ZZYxpE/A6p64p6V38qJcUW5aYz4dEjCzgHpoLEwMpfq+3Ci9U65vCN1U8R1x3/qOrnOIvlXFlALqgaBKbWZYWKM2M2zqSVgXVRJ9AaoQEjCzgHrorkpijf5VxZiDYr+qYkwB9fATGlXkfYtrFUoCZhZQDzRVYjn6+VpFfKvzkNi3qphSQD0QVBHfGjymKG4WC/AdHDdr6ZbKrNqkamZWmguoh8bCxJBYi4v3nrdpFJtNHVGOj/G0HgAiUAUAElAFABJQBQASUAUAElAFABJQBQASmDIPgSAFRhUASEAVAEhAFQBIQBUASEAVAEhAFQBIQBUASEAVAEhAFQBIQBUASEAVAEhAFQBIQBUASEAVAEhAFQBIQBUASEAVAEhAFQBIQBUASEAVAEhAFQBIQBUASEAVAEhAFQBI/B/9cC5JxxW61QAAAABJRU5ErkJggg==" alt="" width="300" />
WPF DataGrid绑定及列居中的更多相关文章
- WPF DataGrid绑定一个组合列
WPF DataGrid绑定一个组合列 前台: <Page.Resources> <local:InfoConverter x:Key="converter& ...
- WPF Datagrid 动态生成列 并绑定数据
原文:WPF Datagrid 动态生成列 并绑定数据 说的是这里 因为列头是动态加载的 (后台for循环 一会能看到代码) 数据来源于左侧列 左侧列数据源 当然num1 属于临时的dome使用 可 ...
- WPF DataGrid 绑定数据及时更新的处理
原文:WPF DataGrid 绑定数据及时更新的处理 默认情况下datagrid 绑定数据源后,在界面编辑某一列后,数据不会及时更新到内存对象中.如在同一行上有一个命令对来获取 当前选中行(内存对象 ...
- WPF DataGrid 绑定行双击行命令
WPF DataGrid 绑定行双击行命令 <DataGrid ...> <DataGrid.InputBindings> <MouseBinding MouseActi ...
- WPF DataGrid添加编号列
WPF DataGrid添加编号列? 第一步:<DataGridTemplateColumn Header="编号" Width="50" MinWidt ...
- WPF DataGrid 绑定DataSet数据 自动生成行号
1.绑定数据:dataGrid1.ItemsSource = dataSet.Tables[0].DefaultView; 注意:在创建DataGrid 时可以通过AutoGenerateColumn ...
- WPF DataGrid支持的列类型
WPF DataGrid支持下面几种列类型: DataGridTextColumn DataGridCheckBoxColumn DataGridComboBoxColumn DataGridHype ...
- WPF datagrid 动态增加列
DataGrid动态增加列 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.m ...
- WPF DataGrid动态生成列的单元格背景色绑定
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Column.DisplayInde ...
随机推荐
- windbg 基础命令实战 - 简单程序破解
以前玩游戏遇到一些实在过不去的管卡,经常会找一些游戏修改软件来修改游戏,让自己变得无比强大,将boss一路砍瓜切菜过足游戏瘾.其实游戏修改软件的功能大多都比较简单,我们可以通过windbg的一些简单命 ...
- http学习笔记(一)
写在前面: 第一次想写系列文章,学习了一些web知识后,发现自己还有很大的不足,但又不知道该学习些什么来完善自己的知识体系,偶然在网上看到了一篇介绍http的文章,觉得对自己有一些帮助,于是想要开始学 ...
- 微软专家推荐11个Chrome 插件
Web开发人员,需要长时间使用浏览器,尽管Windows10 Edge浏览器启动非常快速,且支持110多种设备,Edge支持基于JS 扩展,但也删除了很多旧功能像Active-X等插件.多数情况下,插 ...
- NSString NSUrl 相互转化
//NSString 转为 NSURL NSString *webStr = @"www.baidu.com"; NSURL *webUrl = [NSURL URLWithStr ...
- 我心中的核心组件(可插拔的AOP)~调度组件quartz.net续~任务管理器的开发
回到目录 对于任务调度来说,越来越多的团队选择了quartz,它在java和.net环境下表现都十分优秀,配置简单,功能强大,时间表达式配置灵活,但在使用时,还是感觉缺点什么,怎么说,你在服务器上安装 ...
- 我心中的核心组件~HttpHandler和HttpModule实现图像的缩放与Url的重写
回到目录 说在前 对于资源列表页来说,我们经常会把图像做成N多种,大图,小图,中图等等,很是麻烦,在数据迁移时,更是一种痛快,而如果你把图像资源部署到nginx上,那么这种图像缩放就变得很容易了,因为 ...
- EF架构~在ef中支持IQueryable级别的Contains被翻译成了Exists,性能可以接受!
回到目录 Entityframeworks很聪明 不错,非常不错!ef里的contains比linq to sql里的contains有了明显的提升,事实上,是在进行SQL语句翻译上有所提升,在lin ...
- [Java面试十二]数据库概念相关
1. 什么是存储过程?它有什么优点? 答:存储过程是一组予编译的SQL语句,它的优点有: 允许模块化程序设计,就是说只需要创建一次过程,以后在程序中就可以调用该过程任意次. 允许更快执 ...
- iOS-观察者模式
cocoa框架中很多地方都使用了观察者模式 一.KVO Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知.每次指定的被观察的对象的属性被修改后, ...
- C#教程(1) -- .Net与C#简介
(1).Net .Net指.Net平台或者是.Net Framework框架. 如果你把.Net平台想象成一个厨房,那么.Net Framework框架就是其中的柴米油盐酱醋茶. 如果你把.Net平台 ...