数据绑定与计时器实现

问题描述:

无法弄清楚为什么这个计时器不显示信息。计时器被装配来更新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

你必须这样改变。因为你做错了方式。

  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