wpf 设置新行Datagrid的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12485664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:24:36  来源:igfitidea点击:

Set the content of a new row Datagrid

wpfdatabase

提问by GameAlchemist

I have a DataGrid showing some databases having quite some columns.
I would like that, when the user edit a new row, some values are set automatically.
With the windows form DataGrid that would be easy, since there's RowsAdded event handler. But how could i handle this with the wpf DataGrid ??
Edit : my DataGrid is bound in Xaml to a public property which is an ITable. When user select a table in a ComboBox, the property is updated with corresponding table. Yes there's autogenerating column, and the way the user can enter a new row is to edit the last blank row (default behaviour).

我有一个 DataGrid 显示一些具有相当多列的数据库。
我希望,当用户编辑新行时,会自动设置一些值。
使用 Windows 窗体 DataGrid 会很容易,因为有 RowsAdded 事件处理程序。但是我怎么能用 wpf DataGrid 处理这个?
编辑:我的 DataGrid 在 Xaml 中绑定到一个公共属性,它是一个 ITable。当用户在 ComboBox 中选择一个表格时,该属性会更新为相应的表格。是的,有自动生成列,用户输入新行的方式是编辑最后一个空白行(默认行为)。

采纳答案by GameAlchemist

Ok i think i got it.
When a DataTable is bound to a DataGrid, a CollectionView is created in order to see it. You can get it by using the (static/shared) CollectionView.GetDefaultView(ThePropertyThatIsBound) method.
Since it implements ICollectionChanged, you can add an event handler to the CollectionChangedEvent.

好的,我想我明白了。
当 DataTable 绑定到 DataGrid 时,会创建一个 CollectionView 以便查看它。您可以使用(静态/共享)CollectionView.GetDefaultView(ThePropertyThatIsBound) 方法获取它。
由于它实现了 ICollectionChanged,您可以向 CollectionChangedEvent 添加一个事件处理程序。

In the CollectionChanged event handler, if you have a new item (e.NewItems.Count>0) you must check it against System.Windows.Data.CollectionView.NewItemPlaceholder and if it is not a place holder, then it is a brand new item, for wich i can set all default values.

在 CollectionChanged 事件处理程序中,如果您有一个新项目(e.NewItems.Count>0),您必须对照 System.Windows.Data.CollectionView.NewItemPlaceholder 检查它,如果它不是占位符,则它是一个全新的项目,为此我可以设置所有默认值。

回答by Eirik

You can do this in the LoadingRow event. Try something like this:

您可以在 LoadingRow 事件中执行此操作。尝试这样的事情:

private void myDataGrid_LoadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
{
    MyObject myObject = e.Row.Item as MyObject;
    if (myObject != null)
    {
        myObject.PropertyOne = "test";
        myObject.PropertyTwo = 2;
    }
}

回答by Samidjo

Assign a CollectionViewSourceto your DataGridthen listen to the CollectionChangedevent as following :

分配一个CollectionViewSource给你DataGrid然后听CollectionChanged事件如下:

..
public CollectionViewSource ViewSource { get; set; }
..
this.ViewSource = new CollectionViewSource();
this.ViewSource.Source = new List<YourObjectType>();
this.ViewSource.View.CollectionChanged += View_CollectionChanged;

..

private void View_CollectionChanged(object sender,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    if (e.NewItems.Count > 0)
    {
        YourObjectType myObject = e.NewItems[e.NewItems.Count-1] as YourObjectType;
        if (myObject != null)
        {
            myObject.Property        = TheValueYouWant;
            ..
        }
    }
}
..
<DataGrid ItemsSource="{Binding ViewSource.View}" ../>