I am writing an application that reads information on the form and transforms that data in various ways based on a set of rules. Rather than get into the why's of the scenario, I will just lay out a basic example of what I am trying to do. Please note, this whole business of using PropertyChangedEventHandler and INotifyPropertyChanged is very new to me and I am still reading and learning about it.
Here is a sample class I use to capture the first name and a message to the user:
class Names : INotifyPropertyChanged
{
private string _firstName;
public string firstName { get { return _firstName; } set { SetField(ref _firstName, value, "firstName"); } }
private string _nameMessage;
public string nameMessage { get { return _nameMessage; } set { SetField(ref _nameMessage, value, "nameMessage"); } }
#region handle property changes
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetField<T>(ref T field, T value, string propertyName)
{
//if the value did not change, do nothing.
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
//the value did change, so make the modification.
field = value;
OnPropertyChanged(propertyName);
return true;
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
Whenever the property for firstName changes, I want to automatically modify the value in the nameMessage property.
For example, the user types John , so the nameMessage property is set to Hello John .
This is a very simple example of what I am trying to accomplish but I am getting stuck because it is hard for me to understand how to get the pointer to that particular property for that index.
Here are my two questions: 1) What code should I write to get the changed value and modify it? 2) Does that business logic belong within the class, or should it be in a different class altogether?
Any pointers you can provide would be very helpful.
Thank you.
to cascade a property fro one to another, you could implement your firstName property to be implemented like this:
public string firstName
{
get { return _firstName; }
set
{
if (SetField(ref _firstName, value))
{
// first name changed, so update the other field too
this.nameMessage = string.Format("hello {0}", _firstName);
}
}
}
you could also do this without every actually storing the nameMessage as a field. it could be generated on the fly:
public string nameMessage { get { return string.Format("hello {0}", _firstName); } }
and simply add an additional property change notification to the setter for first name:
set
{
if (SetField(ref _firstName, value))
{
// first name changed, so update the other field too
NotifyPropertyChanged("nameMessage");
}
}
related: one trick is using the CallerMemberName
annotation to provide a default value for the property name, so you don't have to pass that in your setfield
calls
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
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.