1

I have a textbox which Text property is bound like that

 <TextBox Name="txtBox"> <TextBox.Text> <Binding Path="Data"> </Binding> </TextBox.Text> </TextBox> 

The filed Data can be changed in various places in my program. However if I change filed Data in ahother control, the t txtBox Text property does't refresh itself. I still can see the old value(despite the fact that Data filed has been changed). Is there any way to force textbox to refresh itselft or sth?

2 Answers 2

5

In order for your textbox to know when the data it's bound to changes, the class that it is bound to must implement INotifyPropertyChanged. Below is an example in C#. If you bind a textbox to the property Data, the textbox should update when the Data property's setter is executed. Note that INotifyPropertyChanged will require a reference to the System.ComponentModel namespace (a Using in C# or Imports in VB).

public class MyData : INotifyPropertyChanged { string _data; public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public string Data { get { return _data; } set { _data = value; this.OnPropertyChanged("Data"); } } } 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for answer. I'll give it a chance :) However I would like to know if there is any way to refresh textbox.text properryy manually. Sth like textbox.refresh() ??
One more question. Is there any way to implement this interface and use autoproperty ?? Basicly all properties in my class are implemented this way public DateTime Date { get; set; }
Regarding manual refreshing, it depends on whether you want to do it by accessing the control directly from the view or indirectly from the viewmodel. The example I provided indirectly causes the binding to update from the viewmodel. So it comes down to whenever you raise the OnPropertyChanged event. You can raise the event any time you want - it doesn't necessarily have to be in the setter.
Regarding automatic properties, the compiler creates a private backer and implements the getter and setter for you. I don't know of any other way to raise the OnPropertyChanged when the setter is called without implementing the setter yourself.
Once again thanks for answers :) I used INotifyPropertyChange, this is great :)
0

does your Data property owner object implement INotifyPropertyChanged? if no than implemet it and fire PropertyChanged When your set property is called

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.