2015-12-15 59 views
0

無法弄清楚爲什麼這個計時器不顯示信息。計時器被裝配來更新TimeEntry中的TextBlock。綁定似乎不起作用,我不知道如何正確地做到這一點。我查看了MSDN網站。他們只是提供基礎知識:還不夠。數據綁定與計時器實現

代碼:

TimeEntry.xaml.cs

public partial class TimeEntry : UserControl 
{ 
    public static readonly DependencyProperty timeSpentProperty = 
     DependencyProperty.Register("timeSpent", typeof(TimeSpan), 
     typeof(TimeEntry), 
     new FrameworkPropertyMetadata(TimeSpan.Zero)); 

    #region Properties 
    public TimeSpan timeSpent 
    { 
     get 
     { 
      return (TimeSpan)GetValue(TimeEntry.timeSpentProperty); 
     } 
     set 
     { 
      SetValue(TimeEntry.timeSpentProperty, value); 
     } 
    } 
    #endregion 

    static TimeEntry() { } 

    public TimeEntry(int id) 
    { 
     DataContext = this; 
     this.InitializeComponent(); 
     //code 
    } 
} 

TimeEntry.xaml

<UserControl 
    x:Class="ClockWatcher.TimeEntry" 
    x:Name="UserControl"> 
    <Grid x:Name="LayoutRoot" HorizontalAlignment="Left" 
     VerticalAlignment="Top" Width="{DynamicResource creationWidth}" 
     Height="{DynamicResource creationHeight}"> 
     <TextBlock x:Name="timeSpentBlock" 
      HorizontalAlignment="Left" TextWrapping="Wrap" 
      Text="{Binding timeSpent, ElementName=UserControl}" 
      VerticalAlignment="Top" Padding="{StaticResource labelPadding}"/>  
    </Grid> 
</UserControl> 

SessionManager.cs

public class SessionManager : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     [NonSerialized] 
     private Timer _timer; 
     [NonSerialized] 
     private Stopwatch _clockWatch; 
     [NonSerialized] 
     private DateTime _dtStartTime; 
     private Session current_session; 
     public string strStartTime 
     { 
      get 
      { 
       return _dtStartTime.ToString(); 
      } 
      private set { } 
     } 

     public SessionManager() 
     { 
      _clockWatch = new Stopwatch(); 
      _timer = new Timer(1000);//one second 
      _timer.Elapsed += clockWatch_Elapsed; 
      _dtStartTime = DateTime.Now; 
      CurrentSession = new Session(); 
     } 

     /// <summary> 
     /// Registered to Timer.Elapsed Event 
     /// (See constructor) 
     /// </summary> 
     public void clockWatch_Elapsed(object sender, ElapsedEventArgs e) 
     { 
      if (CurrentSession != null) 
      { 
       //update the timespent variable of the current timeEntry 
       if (CurrentSession.currentTimeEntry != null) 
       { 
        CurrentSession.currentTimeEntry.timeSpent = _clockWatch.Elapsed; 
        calculateTotalTime(); 
       } 
      } 
     } 
     private void OnPropertyChanged([CallerMemberName] string member_name = "") 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(member_name)); 
      } 
     } 
    } 
+0

您的任務中的哪部分時間值是?也許那是缺少的一個。 – tgpdyk

+0

@tgpdyk:它在關聯的DependencyProperty中。 –

+0

我的意思是:' tgpdyk

回答

0

你必須這樣改變。因爲你做錯了方式。

  1. 想一想,哪個屬性必須更新。你想要的財產是timeSpent。然後你的TimeEntry類必須實現INotifyPropertyChanged事件。

  2. timeSpent屬性不需要依賴屬性。因爲您在'elapsed'事件中手動分配它。

  3. 您的xaml必須與您的班級相連。換句話說,你的身體(xaml)必須有靈魂(類實例)。

  4. 您已經在步驟3中綁定了模型。現在您可以像這樣簡單綁定了。

enter image description here enter image description here

步驟2.(依賴項屬性)

當前的使用是manually assignment的說明。在你的情況,不需要依賴屬性

enter image description here

如果你要使用像下面的圖片,你的財產timeSpent必須依賴項屬性。

enter image description here

+0

Number 2是什麼意思(手動分配)?什麼時候應該使用DependencyProperties? –

+0

@SageKelly Post已更新 – ebattulga