如何使用NotificationObject在Silverlight MVVM中刷新绑定数据



我正在处理文本框必需的时间板值。输入内容需要进行验证,并且可能以几种不同的格式(对于EX 1300表示13:00)。我做一些工作以检查并在ViewModel中进行转换。但是之后,如何在文本框中刷新文本?

<TextBox Text="{Binding Path= OpenHourFromText, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" ></TextBox>

OpenHourFromValue是我用于验证和数据绑定的字符串属性

    public class MainPageViewModel : NotificationObject{
        public string OpenHourFromText
                {
                    get
                    {
    //OpenHourFrom is a TimeSpan property that contain the value
                        if (OpenHourFrom != null)
                        {
                            return GetOpeningHourText(OpenHourFrom); //fomat the time
                        }
                        else
                        {
                            return "";
                        }
                    }
                    set
                    {
//do validation and convert here. 1300 will be changed to 13:00 TimeSpan type
                        OpenHourFrom = ConvertToTimeSpan(value);  
                        RaisePropertyChanged("OpenHourFromText");
                    }
                }
        public TimeSpan OpenHourFrom { get; set; }
}

ViewModel是从Microsoft.practices.prism.ViewModel.NotificationObject

继承的

在文本框中输入1300后,更新了OpenHourfrom。但是文本框的文本未更改为13:00。为什么?请帮助,许多THX。

当文本框设置某些值时,它不会调用。解决方案可能就像用dispatcher.begininvoke((()=> raisepropertychanged(" openhourfromtext")替换raisepropertychanged(" openhourhourfromtext")));它将延迟发射该事件。

set 
   { 
    //do validation and convert here. 1300 will be changed to 13:00 TimeSpan type 
     OpenHourFrom = ConvertToTimeSpan(value);                                            
     Dispatcher.BeginInvoke(() => RaisePropertyChanged("OpenHourFromText"));
   }

您正在提高属性 UpdateTimeText的属性汇总通知,而实际的属性名称为 OpenHourFromText

更改您的PropertyChange通知以提高正确属性的通知,并应为您更新。

最新更新