开发者

TextBox - binding property doesn't refresh itself

I have a textbox which Text property is bound like that

   <T开发者_Go百科extBox 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?


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");
        }
    }
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜