简体   繁体   English

WPF Datagrid允许用户添加行

[英]WPF Datagrid allow user to add rows

I want to allow the user to add rows in a datagrid. 我想允许用户在数据网格中添加行。 I know the Datagridview from WinForms and there is allways on the bottom of the Datagrid a empty line that i can fill with data. 我知道WinForms的Datagridview,并且在Datagrid的底部始终有一条空行,我可以用数据填充该行。

<DataGrid x:Name="dgv" 
          Grid.ColumnSpan="4" 
          Grid.Row="1" 
          CanUserAddRows="True" 
          CanUserDeleteRows="True" 
          SelectionUnit="FullRow" 
          AutoGenerateColumns="False" 
          ItemsSource="{Binding Entries}">
        <DataGrid.Columns>
            <DataGridComboBoxColumn Header="Anrede" Width="1*"></DataGridComboBoxColumn>
            <DataGridTextColumn Header="Vorname" Width="2*" Binding="{Binding Vorname}" />
            <DataGridTextColumn Header="Nachname" Width="2*" Binding="{Binding Nachname}" />
        </DataGrid.Columns>
    </DataGrid>

Code behind: 后面的代码:

public ObservableCollection<Mitreisender> Entries { get; }

public aufenthaltsWindow()
{
    InitializeComponent();
    Entries = new ObservableCollection<Mitreisender>();
}

The Class 班级

public class Mitreisender
{
    public int MitreisenderID { get; set; }
    public Gast.AnredeTyp Anrede { get; set; }
    public string Titel { get; set; }
    public string Vorname { get; set; }
    public string Nachname { get; set; }

    public virtual Aufenthalt Aufenthalt { get; set; }

    public Mitreisender()
    {

    }
}

You need to set the DataGrid.ItemsSource to a modifyable collection. 您需要将DataGrid.ItemsSource设置为可修改的集合。 Suppose your DataContext contains a property 假设您的DataContext包含一个属性

public ObservableCollection<EntryViewModel> Entries { get; }

with

class EntryViewModel // probably derive some ViewModelBase and implement INotifyPropertyChanged
{
    public string Vorname { get; set; }
    // ... other properties
}

Then bind the collection from datacontext to itemssource. 然后将集合从datacontext绑定到itemssource。

<DataGrid x:Name="dgv" ItemsSource="{Binding Entries}" ...>

With a modifyable collection and your settings, an extra line for new items will be available. 有了可修改的收藏集和您的设置,将有一条新项目的额外行。 You will still need to bind the columns to the actual item properties, I'm not addressing this part here. 您仍然需要将列绑定到实际的项目属性,这里我不在此讨论。

Make sure that the Mitreisender class has a parameterless constructor: 确保Mitreisender类具有无参数的构造函数:

public Mitreisender()
{

}

This is required for the blank "add" row to show up. 这是显示空白“添加”行所必需的。

Also make sure that the binding works and that you have set the DataContext of the DataGrid or its parent window to an instance of the class where the Entries property is defined: 还要确保绑定有效,并且已将DataGrid的DataContext或其父窗口设置为定义了Entries属性的类的实例:

public aufenthaltsWindow()
{
    InitializeComponent();
    Entries = new ObservableCollection<Mitreisender>();
    DataContext = this;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM