简体   繁体   中英

MVVM Update Property A when Property B changes

I have a ComboBox and DataGrid setup like so:

<ComboBox Name="PayrollDatasesListBox"
          IsReadOnly="True"
          ItemsSource="{Binding PayrollDatabasesList}"
          SelectedValue="{Binding SelectedPayrollDatabase}" />

    <DataGrid Name="RateTable"
              ItemsSource="{Binding RateTableDataView}"
              Margin="20"
              AutoGenerateColumns="False"
              Width="775"> ...
    </DataGrid>

In the ViewModel I have the following properties:

    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;
        }
    }

How can I update RateTableDataView when SelectedPayrollDatabase changes?

I might be missing something but you can just update from the setter for your selected payroll database.

Or just call the PropertyChanged event from INotifyPropertyChanged passing your property name.

Something like...

NotifyPropertyChanged(m => m.RateTableDataView);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM