属性B发生变化时,MVVM更新属性A

本文关键字:属性 MVVM 更新 变化 | 更新日期: 2023-09-27 18:18:25

我有一个组合框和数据网格设置如下:

<ComboBox Name="PayrollDatasesListBox"
          IsReadOnly="True"
          ItemsSource="{Binding PayrollDatabasesList}"
          SelectedValue="{Binding SelectedPayrollDatabase}" />
    <DataGrid Name="RateTable"
              ItemsSource="{Binding RateTableDataView}"
              Margin="20"
              AutoGenerateColumns="False"
              Width="775"> ...
    </DataGrid>
在ViewModel中,我有以下属性:
    public ObservableCollection<String> PayrollDatabasesList
    {
        get
        {
            DelphiaConfigModel _config = (App.Current.Resources["Locator"] as ViewModelLocator).SystemSetupViewModel.DelphiaConfigInfo;
            return new ObservableCollection<String>(_config.DatabaseInfo.PayrollDatabaseNames.Split(',')
                .Select(c => c.ToString().Trim())
                .ToList());
        }
    }
    public String SelectedPayrollDatabase { 
        get
        {
            return _SelectedPayrollDatabase;
        }
        set
        {
            if (_SelectedPayrollDatabase != value)
            {
                _SelectedPayrollDatabase = value;
                NotifyPropertyChanged(m => m.SelectedPayrollDatabase);
            }
        }
    }
    public String _SelectedPayrollDatabase;
    public DataView RateTableDataView
    {
        get
        {
            DataSet ds = new DataSet("TempDataSet");
            // Get reference to DelphiaConfigInfo
            DelphiaConfigModel _config = 
                (App.Current.Resources["Locator"] as ViewModelLocator).SystemSetupViewModel.DelphiaConfigInfo;
            using (SQLClass _sql = new SQLClass(_config.DatabaseInfo))
            {
                using (SqlConnection _conn = _sql.Connection)
                {
                    // use the chosen Payroll Database from ComboBox
                    //TODO: set InitialCatalog to selected database
                    SqlConnectionStringBuilder _newConn =
                        new SqlConnectionStringBuilder(_conn.ConnectionString) { InitialCatalog = SelectedPayrollDatabase };
                    _conn.ConnectionString = _newConn.ConnectionString;
                    //_conn.ChangeDatabase("SageHRMS_BKC");
                    SqlCommand _cmd = _conn.CreateCommand();
                    _cmd.CommandType = CommandType.StoredProcedure;
                    _cmd.CommandText = "aem.S_RateTable_SP";
                    SqlDataAdapter _adapter = new SqlDataAdapter(_cmd);
                    _adapter.Fill(ds);
                }
            }
            return ds.Tables[0].DefaultView;
        }
    }

我如何更新RateTableDataView时,SelectedPayrollDatabase的变化?

属性B发生变化时,MVVM更新属性A

我可能错过了一些东西,但你可以从你所选择的工资数据库的setter更新。

或者直接调用INotifyPropertyChanged中的PropertyChanged事件,传递您的属性名称。

之类的…

NotifyPropertyChanged(m => m.RateTableDataView);