wpf:DataGrid使用
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:WpfToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
1. WpfToolkit:DataGrid在TableView.xaml中定义如下
<WpfToolkit:DataGrid x:Name="PlotViewDataGrid"
ItemsSource="{Binding DataGridSource, Mode=TwoWay}" IsReadOnly="True" SelectedItem="{Binding DataGridSelected}"
AutoGenerateColumns="True" AutoGeneratedColumns="PlotViewDataGrid_AutoGeneratedColumns"
Width="685" RowHeight="25" Margin="0" CanUserResizeRows="True" SelectionMode="Single"
SelectedIndex="{Binding DataGridSelectedIndex, Mode=TwoWay}" Grid.Row="0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding GetSelectCommand}"
CommandParameter="{Binding SelectedItem,ElementName=PlotViewDataGrid}" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding DataGridDoubleClickCommand}"
CommandParameter="{Binding SelectedItem,ElementName=PlotViewDataGrid}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<WpfToolkit:DataGrid.RowStyle>
<Style TargetType="{x:Type WpfToolkit:DataGridRow}">
<Setter Property="Background" Value="Red" />
</Style>
</WpfToolkit:DataGrid.RowStyle>
</WpfToolkit:DataGrid>
1.在TableView.xmal.cs文件中PlotViewDataGrid_AutoGeneratedColumns函数可以改变列宽
private void PlotViewDataGrid_AutoGeneratedColumns(object sender, EventArgs e) {
double dgwidth = this.PlotViewDataGrid.Width;
int columnWidth = this.PlotViewDataGrid.Columns.Count;
this.PlotViewDataGrid.HorizontalContentAlignment = (HorizontalAlignment)1;
for (int i = 0; i < this.PlotViewDataGrid.Columns.Count; i++)
{
this.PlotViewDataGrid.Columns[i].Width = new Microsoft.Windows.Controls.DataGridLength(dgwidth / columnWidth);
}
}
2.在TableViewModel.cs文件中
#region DataGridDoubleClickCommand
RelayCommand<object> dataGridDoubleClickCommand = null;
public ICommand DataGridDoubleClickCommand
{
get
{
if (dataGridDoubleClickCommand == null)
{
dataGridDoubleClickCommand = new RelayCommand<object>((p) => OnDataGridDoubleClickCommand(p), (p) => CanDataGridDoubleClickCommand(p));
} return dataGridDoubleClickCommand;
}
} private bool CanDataGridDoubleClickCommand(object p)
{
return true;
} private void OnDataGridDoubleClickCommand(object p)
{
DataGridDoubleClickHandle(p);
}
#endregion
3.DataGrid风格自定义
<WpfToolkit:DataGrid.RowStyle>
<Style TargetType="{x:Type WpfToolkit:DataGridRow}">
<Setter Property="Background" Value="Red" />
</Style>
</WpfToolkit:DataGrid.RowStyle>
4.CanUserResizeRows="True"允许用户调整行高
其他参考内容如下:
(1)自动生成列
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><DataGrid AutoGenerateColumns="True" Name="datagrid" CanUserAddRows="False" MouseDoubleClick="datagrid_MouseDoubleClick"/>
2)取消自动生成列,手动绑定到相应字段
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><DataGrid AutoGenerateColumns="False" Name="datagrid" CanUserAddRows="False" MouseDoubleClick="datagrid_MouseDoubleClick">
<DataGrid.Columns>
<DataGridTextColumn Header="编号" Binding="{Binding ID}"></DataGridTextColumn>
<DataGridTextColumn Header="公司" Binding="{Binding CompanyName}"></DataGridTextColumn>
<DataGridTextColumn Header="固定资产" Binding="{Binding FixedAssets}" Width ="*"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
最后一列设置Width ="*"是为了取消空白列。
(3)后台代码
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->privatevoid Window_Loaded(object sender, RoutedEventArgs e)
{
datagrid.ItemsSource = AccessDAL.OleDbHelper.ExecuteDataTable("SELECT * from Customers").DefaultView;
}
//双击DataGrid,显示相应信息
privatevoid datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DataRowView row = datagrid.SelectedItem as DataRowView;
MessageBox.Show(row["id"].ToString());
}
//如果绑定到对象集合,如ObservableCollection<Employee>,代码如下:
ObservableCollection<Employee> col;
public EmployeeManage()
{
InitializeComponent();
col =new ObservableCollection<Employee>();
col.Add(new Employee() { Id =, Name ="Jim", Salary =2500.50f });
col.Add(new Employee() { Id =, Name ="John", Salary =2600.50f });
datagrid.ItemsSource = col;
}
privatevoid datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
Employee emp=datagrid.SelectedItem as Employee;
MessageBox.Show(emp.Id.ToString());
}
(4)删除选中的多行数据
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->privatevoid Delete_Click(object sender, RoutedEventArgs e)
{
for (int i = datagrid.SelectedItems.Count -; i >=; i--)
{
Good good = datagrid.SelectedItems[i] as Good;
goods.Remove(good);
}
}
wpf:DataGrid使用的更多相关文章
- WPF DataGrid常用属性记录
WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭 ...
- WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL
In my recent codeproject article on the DataGrid I described a number of techniques for handling the ...
- WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.
WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次 悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...
- xceed wpf datagrid
<!--*********************************************************************************** Extended ...
- 获取wpf datagrid当前被编辑单元格的内容
原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...
- WPF DataGrid绑定一个组合列
WPF DataGrid绑定一个组合列 前台: <Page.Resources> <local:InfoConverter x:Key="converter& ...
- WPF DataGrid自定义样式
微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师).下面,找到我的小抄造型的网格.它不是100%全面,但它可以让你走得很远,有一些非常有用的技巧和陷阱. ...
- WPF DataGrid显格式
Guide to WPF DataGrid formatting using bindings Peter Huber SG, 25 Nov 2013 CPOL 4.83 (13 votes) ...
- WPF DataGrid Custommization using Style and Template
WPF DataGrid Custommization using Style and Template 代码下载:http://download.csdn.net/detail/wujicai/81 ...
- 编写 WPF DataGrid 列模板,实现更好的用户体验
Julie Lerman 下载代码示例 最近我在为一个客户做一些 Windows Presentation Foundation (WPF) 方面的工作. 虽然我提倡使用第三方工具,但有时也会避免使用 ...
随机推荐
- 【转】浅谈HTTP中Get与Post的区别
转自:http://www.cnblogs.com/hyddd Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们可以 ...
- c++虚表的使用 通过虚表调用虚函数的演示代码
//演示一下c++如何找到虚表地址vptr以及如何通过虚表调用虚函数 //zhangpeng@myhexin.com 20130811 #include <iostream> using ...
- python中的对象拷贝
python中.进行函数參数传递或者返回值时,假设是一般的变量,会拷贝传递.假设是列表或字典则是引用传递.那python怎样对列表和字典进行拷贝传递呢:标准库的copy模块提供了两个方法:copy和d ...
- cocosbuilder中的Callbacks和sound effects
cocosbuilder3中有增加了 Callback和sound effects 的timeline 这个东西用来在动画播放过程中控制音效和回调动作,非常方便 按住option键(alt), 点击t ...
- HDU1518(dfs)java/ c++
Square Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 解决Android拍照保存在系统相册不显示的问题
可能大家都知道我们保存相册到Android手机的时候,然后去打开系统图库找不到我们想要的那张图片,那是因为我们插入的图片还没有更新的缘故,先讲解下插入系统图库的方法吧,很简单,一句代码就能实现 Med ...
- 【转】Java web 编解码
几种常见的编码格式 为什么要编码 不知道大家有没有想过一个问题,那就是为什么要编码?我们能不能不编码?要回答这个问题必须要回到计算机是如何表示我们人类能够理解的符号的,这些符号也就是我们人类使用的语言 ...
- LSJ_NHibernate第四章 MVC
前言: MVC现在已经成为web开发的一个主流趋势了,还没用过的小伙伴,你们已经落伍了,这里我推荐一篇学习博客 玩转Asp.net MVC 的八个扩展点 代码完全开源,下载地址:https://gi ...
- Path类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- c语言学习之基础知识点介绍(十六):文件操作
一.文件的分类 1.文本文件:打开之后能看得懂的文件 2.二进制文件:打开之后看不懂,类似乱码之类的文件(视频,音频打开之后,能看.听,是应为电脑中装有播放器,播放器中含有解码器). 二.操作文件的步 ...