wpf datagrid mvvm 动态添加行列 可以运行工程
时间: 2023-09-02 09:15:43 浏览: 58
以下是一个简单的示例,演示如何使用MVVM模式动态添加行和列到WPF DataGrid中。在此示例中,我们将使用ObservableCollection作为ViewModel中的数据源,以便在添加或删除行时自动更新DataGrid。
MainWindow.xaml:
```
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Age" Binding="{Binding Age}" />
<!-- additional columns can be added dynamically -->
<DataGridTextColumn Header="{Binding AdditionalColumnHeader}" Binding="{Binding AdditionalColumnValue}" />
</DataGrid.Columns>
</DataGrid>
<Button Content="Add Row" Command="{Binding AddRowCommand}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
<Button Content="Add Column" Command="{Binding AddColumnCommand}" HorizontalAlignment="Left" Margin="100,10,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
```
MainWindow.xaml.cs:
```
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
}
}
```
MainViewModel.cs:
```
public class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
Items = new ObservableCollection<ItemViewModel>();
AddRowCommand = new RelayCommand(AddRow);
AddColumnCommand = new RelayCommand(AddColumn);
}
public ObservableCollection<ItemViewModel> Items { get; set; }
private string additionalColumnHeader = "Additional Column";
public string AdditionalColumnHeader
{
get { return additionalColumnHeader; }
set
{
additionalColumnHeader = value;
OnPropertyChanged(nameof(AdditionalColumnHeader));
}
}
private RelayCommand addRowCommand;
public RelayCommand AddRowCommand
{
get { return addRowCommand; }
set
{
addRowCommand = value;
OnPropertyChanged(nameof(AddRowCommand));
}
}
private RelayCommand addColumnCommand;
public RelayCommand AddColumnCommand
{
get { return addColumnCommand; }
set
{
addColumnCommand = value;
OnPropertyChanged(nameof(AddColumnCommand));
}
}
private void AddRow()
{
Items.Add(new ItemViewModel() { Name = "New Row", Age = 0 });
}
private void AddColumn()
{
// generate a random value for the additional column
Random random = new Random();
foreach (ItemViewModel item in Items)
{
item.AdditionalColumnValue = random.Next(0, 100);
}
AdditionalColumnHeader = "Additional Column " + (Items.Count + 1);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ItemViewModel
{
public string Name { get; set; }
public int Age { get; set; }
public int AdditionalColumnValue { get; set; }
}
```
在此示例中,我们使用了RelayCommand,这是一个自定义实现的ICommand接口,它允许将事件绑定到ViewModel中的方法。在AddRow和AddColumn方法中,我们通过修改ViewModel中的属性来更新DataGrid。
要使用此示例,请确保在项目中添加了RelayCommand类,并在MainWindow.xaml.cs中引用了命名空间。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)