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) 方面的工作. 虽然我提倡使用第三方工具,但有时也会避免使用 ...
随机推荐
- .Net Framwork类库
.NET Framework 类库是一个由类.接口和值类型组成的库,通过该库中的内容可访问系统功能.它是生成 .NET Framework 应用程序.组件和控件的基础.类库中的命名空间和命名空间类别在 ...
- 理解C# Attribute
1.Attribute与Property Attribute是特性,Property是属性. 2.Attribute与注释 注释:是给程序员看的,编译的时候会去掉这些信息,也就是说,程序集中没有注释的 ...
- Linux 内核进程管理之进程ID 。图解
http://www.cnblogs.com/hazir/tag/kernel/ Linux 内核进程管理之进程ID Linux 内核使用 task_struct 数据结构来关联所有与进程有关的数 ...
- find_if函数与partition函数的转换
编写程序,求大于等于一个给定长度的单词有多少.我们还会修改输出,使程序只打印大于等于给定长度的单词. 使用find_if实现的代码如下: #include<algorithm> #incl ...
- ajax技术的基本概述
大家都知道ajax并非一种新的技术,而是几种原有技术的结合体.它由下列技术组合而成. 1.使用CSS和XHTML来表示. 2. 使用DOM模型来交互和动态显示. 3.使用XMLHttpRequest来 ...
- Beyond REST: How to build a HATEOAS API in Java with Spring MVC, Jersey (JAX-RS) and VRaptor
http://zeroturnaround.com/rebellabs/beyond-rest-how-to-build-a-hateoas-api-in-java-with-spring-mvc-j ...
- jQuery Ajax 实例 全解析
jQuery Ajax 实例 全解析 jQuery确实是一个挺好的轻量级的JS框架,能帮助我们快速的开发JS应用,并在一定程度上改变了我们写JavaScript代码的习惯. 废话少说,直接进入正题,我 ...
- 20160327javaweb 之JSP入门
一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写h ...
- 20151224jquery学习笔记---cookie插件
hello,祝自己平安夜快乐. Cookie 是网站用来在客户端保存识别用户的一种小文件.一般来用库可以保存用户登录信息.购物数据信息等一系列微小信息.一. 使用 cookie 插件官方网站: htt ...
- C#数组按值和按引用传递数组区别
C#中,存储数组之类对象的变量并不是实际存储对象本身,而是存储对象的引用.按值传递数组时,程序将变量传递给方法时,被调用方法接受变量的一个副本,因此在被调用时试图修改数据变量的值时,并不会影响变量的原 ...