I am learning and creating my first WPF application attempting to Implement the MVVM design pattern, however I cannot seem to work out why this property is not firing its Set Accessor, so I can make use of the OnPropertyChanged Method I have. Would really appreciate an explanation as to why this is not working as I expected.
The part I do not understand is that in the GetChargeUnits method in the ViewModel I am creating an instance of my Charge Unit Model and setting the Property to be the result of the reader (This reader does return a result) The Property sets fine? But when stepping through it never hits the Set line in the property so I cannot detect if it has changed. The part commented in this method was what I had originally I have tried many combinations.
Please help, thanks
Model:
public class ChargeUnit : INotifyPropertyChanged
{
private string _chargeUnitDescription;
private int _chargeUnitListValueId;
public event PropertyChangedEventHandler PropertyChanged;
public ChargeUnit()
{
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public string ChargeUnitDescription
{
get { return _chargeUnitDescription; }
set
{
_chargeUnitDescription = value;
OnPropertyChanged("ChargeUnitDescription");
}
}
public int ChargeUnitListValueId
{
get { return _chargeUnitListValueId; }
set
{
_chargeUnitListValueId = value;
OnPropertyChanged("ChargeUnitListValueId");
}
}
ViewModel:
public class ClientRatesViewModel
{
private IList<ClientRates> _clientRatesPreAwr;
private IList<ClientRates> _clientRatesPostAwr;
private List<ChargeUnit> _chargeUnits;
private const string _connectionString = @"connectionString....";
public ClientRatesViewModel()
{
_clientRatesPreAwr = new List<ClientRates>
{
new ClientRates {ClientRatesPreAwr = "Basic"}
};
_clientRatesPostAwr = new List<ClientRates>
{
new ClientRates{ClientRatesPostAwr = "Basic Post AWR"}
};
_chargeUnits = new List<ChargeUnit>();
}
public IList<ClientRates> ClientRatesPreAwr
{
get { return _clientRatesPreAwr; }
set { _clientRatesPreAwr = value; }
}
public IList<ClientRates> ClientRatesPostAwr
{
get { return _clientRatesPostAwr; }
set { _clientRatesPostAwr = value; }
}
public List<ChargeUnit> ChargeUnits
{
get { return _chargeUnits; }
set { _chargeUnits = value; }
}
public List<ChargeUnit> GetChargeUnits()
{
using (var connection = new SqlConnection(_connectionString))
{
connection.Open();
using (var command = new SqlCommand("SELECT LV.ListValueId, LV.ValueName FROM tablename", connection))
{
command.CommandType = CommandType.Text;
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
var test = new ChargeUnit();
test.ChargeUnitDescription = reader["ValueName"].ToString();
//_chargeUnits.Add(new ChargeUnit
//{
// ChargeUnitDescription = reader["ValueName"].ToString(),
// ChargeUnitListValueId = (int)reader["ListValueId"]
//});
}
}
}
}
return new List<ChargeUnit>();
}