2017-10-13 11 views
0

結合する方法:私は、フォーム(「dd.MM.yyy」)と時刻の下に日付を組み合わせて、1つのDateTimeオブジェクトに挿入する必要がある日付と時刻WPF

Exemple: 日13/10/2017とTIME :10:3​​0 - >コンバイン日付結果:13/10/2017 10:30

XAML:

 //DATE ("dd.MM.yyy") 
    <DatePicker HorizontalAlignment="Center" 
     Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}"   
     SelectedDate="{Binding DeliveryDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged ,NotifyOnValidationError=True ,TargetNullValue=''}"/> 


     //TIME 
    <TextBox Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}" > 
       <TextBox.Text > 
        <Binding Path="Time" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True" Mode="TwoWay" > 
         <Binding.ValidationRules> 
          <local:DateTimeValidationRule ValidationStep="RawProposedValue"/> 
         </Binding.ValidationRules> 
        </Binding> 
       </TextBox.Text> 
      </TextBox> 

のViewModel:

public DateTime DeliveryDate; 
    private TimeSpan time; 
    public TimeSpan Time 
    { 
     get { return time; } 
     set 
     { 
      time = value; 
      OnPropertyChanged("Time"); 
     } 
    } 


    public DateViewModel() 
    { saveDate = new RelayCommand<string>(SaveDateFunction); 
     DeliveryDate = DateTime.Now.Date ; 

     } 

    public void SaveDateFunction(string obj)   
    { 
     DateTime combined = DeliveryDate.Add(Time); 
    } 

エラーが発生しました:13/10/2017 00:00:00 どうすれば修正できますか?

答えて

0

SaveDateFunction法に

public void SaveDateFunction(string obj)   
{ 
    DateTime combined = DeliveryDate.AddMilliseconds(Time.TotalMilliseconds); 
} 

を次のシグネチャを試してみては実施例にhere

DateTime d=DateTime.Now.Date; 
TimeSpan t = DateTime.Now.TimeOfDay; 
DateTime combined = d.AddMilliseconds(t.TotalMilliseconds); 
+0

おかげで多くのことを試すことができ、それは動作します:) – TunNet

0

TimeSpanが構造体であり、nullの代わりにデフォルト値を持つため、時間は設定されませんが、NullReferenceExceptionが得られません。

コンバータが必要なため、UIからプロパティに値が渡されません。私はあなたが出力ウィンドウでエラーを見ることができると確信しています。

public class StringToTimeSpanConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     //Your code here 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

DeliveryDateはフィールドでありプロパティではないため、バインディングも機能しません。

関連する問題