属性B更改时的MVVM更新属性A.

时间:2014-08-15 20:23:17

标签: c# wpf mvvm

我有一个ComboBox和DataGrid设置如下:

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

如何在SelectedPayrollDatabase更改时更新RateTableDataView?

1 个答案:

答案 0 :(得分:0)

我可能会遗漏一些内容,但您可以从所选工资核算数据库的setter更新。

或者只是从INotifyPropertyChanged调用PropertyChanged事件传递你的属性名称。

像...一样的东西。

NotifyPropertyChanged(m => m.RateTableDataView);