I am having some trouble with initializing a new variable as the object being sent by the global property changed. I have two classes BeltConfiguration
and BeltProperty
(both classes have INotifyPropertyChanged
). I have a globalpropertychanged
method in the BeltConfiguration
class as seen here.
private void BeltProperty_GlobalPropertyChanged(object sender, PropertyChangedEventArgs e)
{
BeltProperty validBelt = sender as BeltProperty;
if (validBelt != null)
{
this.Validation = validBelt.Validation;
}
switch (e.PropertyName)
{
case "Value":
this.Validation.ValidState = ConfigurationValid.Unknown;
OnPropertyChanged("Validate");
break;
case "ConfigurationInvalid":
this.Validation.ValidState = ConfigurationValid.False;
OnPropertyChanged("Validate");
break;
}
}
In the BeltProperty
class, I call this with OnGlobalPropertyChanged("ConfigurationInvalid");
However, when I call it, no matter what I do, validBelt
always results in being null
. I looked at the object sender by stepping through the code and it says that the declaring method, GenericParametersAttributes
, and GenericParametersPosition
threw an exception of System.InvalidOperationException
. I don't know if that has anything to do with why validBelt
won't accept sender as BeltProperty
. Thanks for any help or advice you can give me.
This is where I called BeltProperty_GlobalPropertyChanged in the Belt Property class.
private ConfigurationValidation _Validation = new ConfigurationValidation(ConfigurationValid.Unknown, "", "", null);
/// <summary>
/// Stores information as to wether this belt property is valid or invalid, and the details.
/// </summary>
internal ConfigurationValidation Validation
{
get { return _Validation; }
set {
_Validation = value;
if(_Validation.ValidState == ConfigurationValid.False)
{
OnGlobalPropertyChanged("ConfigurationInvalid");
}
}
}
/// <summary>
/// A global on property change that runs for any instantiated object of this type.
/// </summary>
/// <param name="name"></param>
static void OnGlobalPropertyChanged(string name)
{
GlobalPropertyChanged(
typeof(BeltProperty),
new PropertyChangedEventArgs(name));
}
Since you are using a safe cast here:
BeltProperty validBelt = sender as BeltProperty
When validBelt
is null
after this assignment it means sender cannot be cast to an instance of BeltProperty
.
Looking at your calling code it looks like you are not passing an instance of BeltProperty
into your event handler.
Assuming GlobalPropertyChanged
is your PropertyChangedEventHandler
delegate change your OnGlobalPropertyChanged
code to this:
/// <summary>
/// A global on property change that runs for any instantiated object of this type.
/// </summary>
/// <param name="name"></param>
static void OnGlobalPropertyChanged(string name)
{
GlobalPropertyChanged(
this,
new PropertyChangedEventArgs(name)
);
}
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.