Silverlight/WP7:
I have a list of objects that I have deserialized from a JSON query. This list will be bound to a ListBox in XAML. However, I want to edit one of the strings that is returned before it gets bound to the UI. I did some searching and I came across this solution, however I am not very clear on the solution proposed:
string _value;
public string Value { get { return _value; } set { _value = value; NotifyPropertyChanged("Value"); NotifyPropertyChanged("ValueFormatted"); } }
public string ValueFormatted { get { return "Static Text: " + _value; } }
What is NotifyPropertyChanged? I am fairly new to开发者_C百科 Silverlight/C# and I only see the INotifyPropertyChanged interface. How do I implement this solution?
The problem you are trying to solve does not actually require the INotifyPropertyChanged interface. It will work fine if you remove the NotifyPropertyChanged methods and INotifyPropertyChanged interface then bind to the ValueFormatted Property.
However implementing INotifyPropertyChanged on classes being bound to the UI is generally a pretty good idea:
"INotifyPropertyChanged interface is used to notify that a property has been changed and thus to force the bound objects to take the new value."
This basically means if you change properties of objects after they have been bound the values will be reflected in your UI.
The Silverlight show gives a nice simple example of INotifyPropertyChanged
You need to implement the INotifyPropertyChanged object on your interface. The NotifyPropertyChanged you see there is just a method call that raises the PropertyChanged event.
The MSDN page on INotifyPropertyChanged actually has a pretty good sample. Once you implement INotifyPropertyChanged, you'll be able to do this no problem. The sample even has the NotifyPropertyChanged method, same as your solution fragment.
You look like you want a property converter, you can specify a converter in your xaml markup, then when the content is being drawn it is first sent to your converter class and the return value is used instead of the original value.
I'm not at home at the moment but should be in an hour or so and will update this with an example.
精彩评论