博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DataGrid:Maintaining an Empty Row for the Entry of New Records
阅读量:5298 次
发布时间:2019-06-14

本文共 2616 字,大约阅读时间需要 8 分钟。

The final (and generally most user-friendly) method is to automatically maintain an empty row as the

last row in the DataGrid. The user can enter data for a new item in this row, and the DataGrid should
automatically add a new empty row once they do. Sadly, there is no built-in feature like this in the core
Silverlight DataGrid. You could add it yourself (all the source code for the DataGrid control is available in
the Silverlight Toolkit), but it’s not a great idea, as you’d be tying yourself to that particular version of the
DataGrid control, and wouldn’t be able to (easily) take advantage of new features and bug fixes in future
releases of Silverlight and the Silverlight Toolkit. You can, however, handle a number of events raised by
the DataGrid control and manage this automatically. The steps are as follows:
1. Maintain a class-level variable that will reference the new item object:
private object addRowBoundItem = null;
2. Add a new item to the bound collection before (or shortly after) binding, and
assign this item to the class-level variable. If binding the DataGrid directly to a
DomainDataSource control, you would do this in the LoadedData event of the
DomainDataSource control, like so:

DomainDataSourceView view = productDataGrid.ItemsSource as DomainDataSourceView;

addRowBoundItem = new Product();
view.Add(addRowBoundItem);
3. Handle the RowEditEnded event of the DataGrid control. If the row being
committed is the empty row item that was edited (you can get the item in the
collection that it is bound to from the DataContext property of the row), then
it’s time to add a new item to the end of the bound collection, ensure it is
visible, select it, and put it in edit mode. For example:

private void productDataGrid_RowEditEnded(object sender,

DataGridRowEditEndedEventArgs e)
{
if (e.EditAction == DataGridEditAction.Commit)
{
if (e.Row.DataContext == addRowBoundItem)
{
DomainDataSourceView view =
productDataGrid.ItemsSource as DomainDataSourceView;
addRowBoundItem = new Product();
view.Add(addRowBoundItem);
productDataGrid.SelectedItem = addRowBoundItem;

productDataGrid.CurrentColumn = productDataGrid.Columns[0];

productDataGrid.ScrollIntoView(addRowBoundItem,
productDataGrid.CurrentColumn);
productDataGrid.BeginEdit();
}
}
}

4. Remember to always delete the last item in the collection before submitting

the changes back to the server (as it will always be the item representing the
new row):
DomainDataSourceView view = productDataGrid.ItemsSource as DomainDataSourceView;
view.Remove(addRowBoundItem);

转载于:https://www.cnblogs.com/Ken-Cai/archive/2012/06/17/2552785.html

你可能感兴趣的文章
WPF中Image显示本地图片
查看>>
SVN版本管理
查看>>
哈希表等概率情况下查找成功和查找不成功的平均查找长度的计算
查看>>
Windows Phone 7你不知道的8件事
查看>>
数据结构(三十七)查找的基本概念
查看>>
Java基础(十六)断言(Assertions)
查看>>
脚本删除文件下的文件
查看>>
实用拜占庭容错算法PBFT
查看>>
笔试题资源整理(1)
查看>>
ubuntu16.04 anaconda3安装
查看>>
css 外边距,内边距的使用
查看>>
关于窗口Y坐标的小问题
查看>>
php gd 图像翻转,php(gd库)输出中文图像的转换函数
查看>>
java 表头添加复选框,表头带有CheckBox可以实现全选的jtable
查看>>
java b组 小计算器,简单计算器..
查看>>
php server port,$_SERVER[‘SERVER_PORT’]关于php5.2一个bug
查看>>
php 类 init,PHP内核探索:类的定义
查看>>
java的二叉树树一层层输出,Java构造二叉树、树形结构先序遍历、中序遍历、后序遍历...
查看>>
meep php,麻省理工时域差分软件 MEEP windows 下编译开发(一)——准备工作
查看>>
matlab的清除0,matlab中的平均值clear %清除变量dx=0.01*2*pi; %间隔x=0:dx:2*pi; %自变量向量y=...
查看>>