aiyagaze
aiyagaze

Reputation: 884

How to refresh binding data if i change it in setter with different value

i need to change the format of a textbox in silverlight. the data is binding via MVVM.

for ex there is a int property, i add 1 to the value in setter and call OnPropertyChanged. I suppose if i input 1 in the textbox and lost foucs, the text will change to 2. But actully the text is not changed, still 1.

<TextBox Name="txtTime" Text="{Binding PersonID, Mode=TwoWay}" />

        private int _personID;
        public int PersonID
        {
            get 
            {
                return _personID;
            } 
            set
            {
                    _personID = value + 1;
                    OnPropertyChanged("PersonID");
             } 
        }

is there anything wrong? what's the right way to do this?

many thanks

Upvotes: 2

Views: 335

Answers (1)

Sai
Sai

Reputation: 176

When TextBox is setting some value it won't call get.The solution to this can be like replacing OnPropertyChanged("PersonID") with Dispatcher.BeginInvoke(() => OnPropertyChanged("PersonID"));It will delay firing that event.

public int PersonID
{
        get
        {
            return _personID;
        } 
        set
        {
            _personID = value + 1;
            Dispatcher.BeginInvoke(() => OnPropertyChanged("PersonID"));
        }
}

Hope this answers your question

Upvotes: 2

Related Questions