【问题标题】:Datagrid not updating with dialogbox button commandDatagrid 不使用对话框按钮命令更新
【发布时间】:2023-01-09 21:27:15
【问题描述】:

总而言之,我正在开发 WPF 应用程序。我在其中使用数据网格并将其绑定到 Icollection 客户集合。我正在使用 MVVM。

我有一个按钮来添加一个新客户,单击它会显示一个对话框。通过该对话框,我将数据保存到我的 SQL 服务器数据库中。一切正常,但是当对话框关闭时( CloseAction(); )。数据网格不更新。我应该怎么办?当我返回到任何其他菜单项并单击客户时,Datagrid 会更新,同时我在构造函数和命令执行中调用相同的函数。 附上图片供参考 任何解决方案将不胜感激。

public CustomerViewModel()
        {            
            ShowNewCustomerWindowCommand = new ViewModelCommand(ExecuteShowNewCustomerWindowCommand);
            SearchCustomerCommand = new ViewModelCommand(ExecuteSearchCustomerCommand);
            GetData();            
        }

protected void GetData()
        {
            customer = new ObservableCollection<CustomerModel>();
            customer = customerRepository.GetByAll();
            customerCollection = CollectionViewSource.GetDefaultView(customer);
            customerCollection.Filter = FilterByName;
            customerCollection.Refresh();
            RaiseProperChanged();           
        }
private void ExecuteShowNewCustomerWindowCommand(object obj)
        {
            var addNewCustomer = new AddNewCustomer();
            addNewCustomer.ShowDialog();
        }

private void ExecuteSaveCustomerCommand(object obj)
        {
            customerModel.FirstName = FirstName;
            customerModel.LastName = LastName;
            customerModel.Contact = Contact;
            customerModel.Address = Address;
            customerRepository.Add(customerModel);
            CloseAction();
            GetData();
        }

【问题讨论】:

    标签: c# wpf mvvm observablecollection


    【解决方案1】:

    我只是在猜测,因为您没有发布任何 xaml 或属性。我假设您至少有一个公共财产 CustomerCollection 并且您的数据网格已绑定到它。我会将代码更改为以下内容:

    public CustomerViewModel()
    {     
       customer = new ObservableCollection<CustomerModel>(); 
       customerCollection = CollectionViewSource.GetDefaultView(customer);
       customerCollection.Filter = FilterByName;  
    
            ShowNewCustomerWindowCommand = new ViewModelCommand(ExecuteShowNewCustomerWindowCommand);
            SearchCustomerCommand = new ViewModelCommand(ExecuteSearchCustomerCommand);
            GetData();            
        }
    
    public ICollectionView CustomerCollection {get; init;}
    
    protected void GetData()
        {
            customer.Clear(); 
            customer.AddRange(customerRepository.GetByAll());
           
            customerCollection.Refresh();
         
        }
    private void ExecuteShowNewCustomerWindowCommand(object obj)
        {
            var addNewCustomer = new AddNewCustomer();
            addNewCustomer.ShowDialog();
        }
    
    
    private void ExecuteSaveCustomerCommand(object obj)
        {
            customerModel.FirstName = FirstName;
            customerModel.LastName = LastName;
            customerModel.Contact = Contact;
            customerModel.Address = Address;
            customerRepository.Add(customerModel);
            CloseAction();
            GetData();
        }
    

    xaml

    <DataGrid ItemsSource="{Binding CustomerCollection, Mode=OneWay}"
    
    • 在 ctor 中初始化 ObservableCollection 和 ICollectionView 一次
    • 使用 .Clear() 而不是新实例
    • addrange 添加 customerRepository.GetByAll()

    【讨论】:

      猜你喜欢
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-25
      • 2014-04-16
      • 2012-11-24
      • 1970-01-01
      • 2014-01-28
      相关资源
      最近更新 更多