2016-09-08 31 views
0

我目前正在研究wpf項目,其中我必須顯示更新客戶的日期。所以,我有webservice方法來更新整個客戶的信息,但日期不變。我認爲可能有字符串格式的問題,但我已經嘗試了一切。請幫忙! 謝謝!如何在wpf中更新日期

這裏是日期選擇

<Label Grid.Column="9" Grid.Row="1" VerticalAlignment="Top"> 
    <DatePicker 
     x:Name="newtally" 
     Text="{Binding CustomerLastTally}" 
     Margin="0 0 0 0" 
     SelectedDateChanged="newtallydate" 
     /> 
</Label> 

& XAML代碼在這裏是C#

private void newtallydate(object sender, SelectionChangedEventArgs e) 
{ 
    webHandler.POS_Update_CustomerAsync(
     int.Parse(selected.Customerid), 
     selected.CustomerName, 
     Decimal.Parse(selected.Customerbalance), 
     selected.CustomerLocation, 
     selected.CustomerLastTally, 
     (selected.Customerphone), 
     (selected.Customeraddress)); 
} 
+0

您沒有使用任何方式或綁定到文本(CustomerLastTally)的值選定的日期,我認爲你需要提供更多的代碼並告訴我們你預期會發生什麼。推測你打算將CustomerLastTally綁定到selected.CustomerLastTally?父數據上下文是什麼? –

+0

嘗試綁定到DatePicker的'SelectedDate'屬性而不是'Text':'SelectedDate =「{Binding CustomerLastTally}」' –

回答

0

使用雙向綁定。

下面是我的ViewModel類

public class UpdateDateViewModel:INotifyPropertyChanged 
{ 
    private DateTime _customerTally; 

    public UpdateDateViewModel() 
    { 
     CustomerLastTally = DateTime.Today.AddDays(-1); 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 

    [NotifyPropertyChangedInvocator] 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     var handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    public DateTime CustomerLastTally 
    { 
     get { return _customerTally; } 
     set 
     { 
      if (_customerTally != value) 
      { 
       _customerTally = value; 
       updateWebservice(); 
       OnPropertyChanged("CustomerLastTally"); 
      } 
     } 
    } 

    private void updateWebservice() 
    { 

    } 
} 

這裏是視圖代碼..

<Grid> 
    <DatePicker 
    x:Name="newtally" 
    Text="{Binding CustomerLastTally,Mode=TwoWay}" 
    Margin="0 0 0 0" 
    /> 
</Grid> 
+0

感謝它的工作 – Hamza

0

使用SelectedDateDatePicker物業Web服務方法..

<DatePicker x:Name="newtally" SelectedDate="{Binding CustomerLastTally}" 
      Margin="0 0 0 0" SelectedDateChanged="newtallydate" />