0
DataGrid
をにバインドしました。期待どおりに列を生成します。私の問題は、ユーザーに空の行を追加させたいということです。どうやってやるの?WPF DataGridで空白行を追加できない
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<DataGrid AutoGenerateColumns="True"
ItemsSource="{Binding People}"
CanUserAddRows="True"/>
</StackPanel>
</Window>
public class Person
{
public Person(string firstName, string lastName)
{
Firstname = firstName;
Lastname = lastName;
}
public string Firstname { get; set; }
public string Lastname { get; set; }
}
public sealed class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<Person> people = new ObservableCollection<Person>
{
new Person("Jack", "Shephard"),
new Person("John", "Locke")
};
public ObservableCollection<Person> People
{
get { return people; }
set
{
people = value;
OnPropertyChanged("People");
}
}
private void OnPropertyChanged(string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var viewModel = new ViewModel();
DataContext = viewModel;
}
}
[ 'CanUserAddRows'の場合:ここで
は私の完全なコードです](https://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.canuseraddrows(v = vs.110).aspx)がtrue(デフォルト値)に設定されている場合、ユーザーは既に新しい行を追加するが、それはparamterlessコンストラクタを必要とする。またはObservableCollectionに空の 'Person'を追加してください – dkozl