wpf datagrid自动添加新行
时间: 2023-09-21 16:05:37 浏览: 63
WPF DataGrid可以通过设置属性来实现自动添加新行。具体步骤如下:
1. 在DataGrid控件中添加一个新行:
<DataGrid ItemsSource="{Binding MyData}" AutoGenerateColumns="False" CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Column 1" Binding="{Binding Column1}" />
<DataGridTextColumn Header="Column 2" Binding="{Binding Column2}" />
</DataGrid.Columns>
</DataGrid>
2. 在ViewModel中初始化数据源:
public class ViewModel
{
public ObservableCollection<MyDataModel> MyData { get; set; }
public ViewModel()
{
MyData = new ObservableCollection<MyDataModel>();
//添加默认行
MyData.Add(new MyDataModel());
}
}
3. 在MyDataModel中设置自动添加新行:
public class MyDataModel : INotifyPropertyChanged
{
//实现INotifyPropertyChanged接口
public event PropertyChangedEventHandler PropertyChanged;
//两列数据
private string _column1;
public string Column1
{
get { return _column1; }
set
{
_column1 = value;
RaisePropertyChanged("Column1");
//添加新行
if (string.IsNullOrEmpty(value) && string.IsNullOrEmpty(Column2))
{
RaiseRequestNewRow();
}
}
}
private string _column2;
public string Column2
{
get { return _column2; }
set
{
_column2 = value;
RaisePropertyChanged("Column2");
//添加新行
if (string.IsNullOrEmpty(value) && string.IsNullOrEmpty(Column1))
{
RaiseRequestNewRow();
}
}
}
//请求添加新行事件
public event EventHandler RequestNewRow;
private void RaiseRequestNewRow()
{
EventHandler handler = RequestNewRow;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
//通知数据变更
private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
4. 在MainWindow中订阅RequestNewRow事件:
public MainWindow()
{
InitializeComponent();
var viewModel = new ViewModel();
this.DataContext = viewModel;
viewModel.MyData.CollectionChanged += MyData_CollectionChanged;
}
private void MyData_CollectionChanged(object sender,
System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
//添加新行
if (e.NewItems != null && e.NewItems.Count > 0)
{
var newItem = e.NewItems[0] as MyDataModel;
newItem.RequestNewRow += NewItem_RequestNewRow;
}
}
}
private void NewItem_RequestNewRow(object sender, EventArgs e)
{
var viewModel = this.DataContext as ViewModel;
if (viewModel != null)
{
viewModel.MyData.Add(new MyDataModel());
}
}
这样设置后,当用户在最后一行输入完成后,DataGrid会自动添加一个新行。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)