DataGrid relocate将新行添加到顶部 [英] DataGrid relocate add new row to the top

查看:166
本文介绍了DataGrid relocate将新行添加到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在DataGrid视图中,空白行添加条目很方便,但是当列表很大时会很快丢失。您可以从DataGrid视图的底部到顶部更改默认位置吗?

In a DataGrid view, the blank row add entries is convenient but quickly get's lost when the list is large. Can you change it's default location from the bottom to the top of the DataGrid view?

推荐答案

我很少使用 DataGrid 并且一无所知,它能够添加行,但是从WPF / MVVM的角度来看,您不需要这样做。通常在WPF / MVVM中,我们操纵数据而不是UI控件,因此解决方案非常简单。首先,我们将绑定到我们的数据集合(以任何形式我们选择)到 DataGrid.ItemsSource 属性:

I've rarely used a DataGrid and know nothing about its ability to add rows, but coming from a WPF/MVVM point of view, you don't need that anyway. Generally in WPF/MVVM, we manipulate data rather than UI controls, so the solution is easy. First we Bind our collection of data (in whatever shape we chose) to the DataGrid.ItemsSource property:

public ObservableCollection<DataItem> SomeDataCollection { get; set; }

...

<DataGrid ItemsSource="{Binding SomeDataCollection}" ... />

现在,如果我们要在的底部添加一个新项目, DataGrid 我们可以在代码背后/视图模型中执行此操作:

Now, if we want to add a new item to the bottom of the DataGrid we can do this in the code behind/view model:

SomeDataCollection.Add(new DataItem());

然后,如果我们要添加一个新项目到集合的开始,我们可以这样做:

Then if we want to add a new item to the start of the collection, we can just do this:

SomeDataCollection.Insert(0, new DataItem());

当然,您需要实现 INotifyPropertyChanged 接口在你的代码背后/视图模型,以使这项工作,但(希望),你会这样做。无论如何。

Of course, you'll need to implement the INotifyPropertyChanged interface in your code behind/view model to make this work, but (hopefully) you'll be doing that anyway.

更新>>>

对不起,我误解了你。我发现一个 NewItemPlaceholderPosition 在MSDN上由。 c code> ItemCollection.IEditableCollectionView.NewItemPlaceholderPosition 属性。不幸的是,这些链接的页面没有代码示例,所以我在在wpf datagrid如何获得空白行顶部?在StackOverflow上发布。从@woodyiii在那篇文章中的答案:

Sorry, I misunderstood you. I found a NewItemPlaceholderPosition Enumeration on MSDN that is used by an ItemCollection.IEditableCollectionView.NewItemPlaceholderPosition Property. Unfortunately, those linked pages don't have an code examples, so I found one in the in wpf datagrid how to get the blank row on top? post here on StackOverflow. From the answer by @woodyiii in that post:

var view = CollectionView.GetDefaultCollectionView(EmployeeList)
             as IEditableCollectionView;
if(view!=null)
  view.NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtBeginning;

这的确意味着你必须使用一个 CollectionView 让这个工作,但它似乎是唯一的方式... NewItemPlaceholderPosition 除了各种<$ c $以外的任何东西都不使用枚举c> CollectionView 类。

This does mean that you'd have to use a CollectionView to get this to work, but it seems like the only way... the NewItemPlaceholderPosition Enumeration isn't used by anything apart from the various CollectionView classes.

这篇关于DataGrid relocate将新行添加到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆