MVVM模式删除新增修改DataGrid内容

img


用MVVM模式:可通过button控件完成对DataGrid数据的增删改查,数据可本地存储为xml文件

源代码提供下

好的,使用MVVM模式实现按钮控制DataGrid数据增删改查并存储为xml文件的代码示例如下:

<!-- View -->
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
        <Button Command="{Binding AddCommand}" Content="Add"/>
        <Button Command="{Binding DeleteCommand}" Content="Delete"/> 
        <Button Command="{Binding EditCommand}" Content="Edit"/>
        <Button Command="{Binding SaveCommand}" Content="Save"/>
    </StackPanel>
    
    <DataGrid ItemsSource="{Binding Users}" Grid.Row="1"/>

</Grid>
// ViewModel
public class MainViewModel : INotifyPropertyChanged 
{
    public ICommand AddCommand {get;set;} 
    public ICommand DeleteCommand {get;set;}
    public ICommand EditCommand {get;set;}
    public ICommand SaveCommand {get;set;}

    private ObservableCollection<User> _users;
    public ObservableCollection<User> Users 
    {
        get { return _users; }
        set 
        {
            _users = value;
            OnPropertyChanged();
        }
    }

    public MainViewModel() 
    {
        // 初始化Commands
        AddCommand = new RelayCommand(OnAdd);
        DeleteCommand = new RelayCommand(OnDelete);
        EditCommand = new RelayCommand(OnEdit);
        SaveCommand = new RelayCommand(OnSave);

        // 初始化Users
        _users = new ObservableCollection<User>();
        // 加载本地xml数据
        LoadData(); 
    }

    private void OnAdd()
    {
        // 新增用户逻辑
    }

    private void OnDelete()
    {
        // 删除用户逻辑 
    }

    private void OnEdit()
    {
        // 编辑用户逻辑
    }

    private void OnSave()
    {
        // 保存用户集合到xml文件
    }
    
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

}

以上通过命令模式来调用对应函数实现增删改查逻辑,使用MVVM的ObservableCollection作为数据绑定源,并在保存时序列化到xml文件。这样可以利用MVVM模式使用简洁的代码实现功能需求。

完美实现

MVVM模式删除新增修改DataGrid内容的具体代码实现,可以参考一些内容:
WPF MVVM模式下DataGrid动态增加列与删除列:https://blog.51cto.com/u_15127604/3364382
MVVM模式删除新增修改DataGrid内容:https://blog.csdn.net/qq_43320293/article/details/131684994

MVVM模式删除新增修改DataGrid内容
可以参考下


https://www.saoniuhuo.com/question/detail-2635380.html

你原来的源代码呢

public class Product {
    private int productId;
    private String productName;
    private double productPrice;

    public Product(int productId, String productName, double productPrice) {
        this.productId = productId;
        this.productName = productName;
        this.productPrice = productPrice;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public double getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(double productPrice) {
        this.productPrice = productPrice;
    }
}

MVVM(Model-View-ViewModel)模式是一种用于构建用户界面的软件设计模式,它能够有效地将数据、用户界面和业务逻辑分离。在MVVM模式中,View负责呈现用户界面,Model负责存储数据,而ViewModel则充当View和Model之间的桥梁。

当涉及到处理DataGrid(数据表格)的内容时,MVVM模式同样可以提供良好的支持。本文将讨论如何使用MVVM模式来实现删除、新增和修改DataGrid内容。

删除内容:
MVVM模式中,删除DataGrid中的内容通常涉及以下几个步骤:

在ViewModel中,维护一个与DataGrid内容对应的数据集合,例如使用ObservableCollection。
在View中,将DataGrid的ItemsSource绑定到ViewModel中的数据集合,并通过数据绑定将数据呈现在DataGrid中。
当需要删除内容时,通过交互操作(例如按下删除按钮)触发Command命令,该命令会在ViewModel中执行相应的删除逻辑。
在ViewModel中的删除逻辑中,更新数据集合(例如调用Remove方法),并确保数据绑定机制能够自动刷新DataGrid中的显示内容。
新增内容:
要新增DataGrid中的内容,可以按照以下步骤进行操作:

在ViewModel中,定义用于新增内容的属性和命令。
在View中,通过数据绑定将新增操作的触发事件(例如按钮点击)与ViewModel中的命令进行绑定。
在ViewModel中的新增命令中,执行逻辑以创建新的数据对象,并将其添加到数据集合中。
数据绑定机制会自动刷新DataGrid以显示新增内容。
修改内容:
修改DataGrid中的内容也可以遵循类似的流程:

在ViewModel中,定义用于修改内容的属性和命令。
在View中,将需要修改的元素绑定到ViewModel中的属性上。可以使用双向数据绑定,以便在View中修改的值能够同步到ViewModel中。
当用户在View中修改内容时,ViewModel中的属性会自动更新。可以在ViewModel中的修改命令中执行相应的逻辑,例如更新数据库或将修改后的值保存到文件中。
数据绑定机制会自动刷新DataGrid以显示修改后的内容。
在实现删除、新增和修改DataGrid内容时,MVVM模式能够提供以下好处:

通过将界面逻辑从业务逻辑中分离,提高代码的可测试性和可维护性。
通过数据绑定和命令绑定,简化了界面和逻辑之间的交互过程。
通过使用数据集合和属性绑定,实现了数据的自动刷新和同步。
需要注意的是,在实现MVVM模式时,建议使用合适的框架或库(如WPF、MVVMLight、Prism等),它们提供了许多便利的功能来简化开发过程。

总结起来,MVVM模式能够很好地支持删除、新增和修改DataGrid内容。它提供了一种结构清晰、代码可测试和可维护性高的方式来处理用户界面和数据的交互。通过使用MVVM模式,您可以更轻松地开发出功能齐全的DataGrid应用程序。