1

私はテキストボックスに必要なTimeSpan値に取り組んでいます。入力コンテンツは検証する必要があり、いくつかの異なる形式である可能性があります(たとえば、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; }

}

ビューモデルはMicrosoft.Practices.Prism.ViewModel.NotificationObjectから継承されます

テキストボックスに1300を入力すると、OpenHourFromが更新されます。ただし、テキストボックスのテキストは13:00に変更されません。なぜ?助けてください、多くのthx。

4

2 に答える 2

1

実際のプロパティUpdateTimeText名がOpenHourFromText

通知を変更してPropertyChange、正しいプロパティの通知を発行すると、更新されます。

于 2012-10-22T16:28:37.017 に答える
1

TextBoxが値を設定している場合、getは呼び出されません。これに対する解決策は、RaisePropertyChanged( "OpenHourFromText")をDispatcher.BeginInvoke(()=> RaisePropertyChanged( "OpenHourFromText"));に置き換えるようなものです。 。

set 
   { 
    //do validation and convert here. 1300 will be changed to 13:00 TimeSpan type 
     OpenHourFrom = ConvertToTimeSpan(value);                                            
     Dispatcher.BeginInvoke(() => RaisePropertyChanged("OpenHourFromText"));
   }
于 2012-10-23T14:38:46.260 に答える