定时器实现的数据绑定

Data Binding with Timer Implementation

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

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

  1. 想一想,属性必须更新。你想要的 属性 是 timeSpent。那么你的 TimeEntry class 必须实现 INotifyPropertyChanged 事件。

  2. timeSpent 属性 不需要依赖 属性。因为你在 'elapsed' 事件中手动分配它。

  3. 您的 xaml 必须与您的 class 连接。换句话说,你的 body(xaml) 必须有 soul(class 实例)。

  4. 您已经在步骤 3 中绑定了模型。现在您可以像这样简单地绑定。

步骤 2 的说明。(依赖性 属性)

您当前的使用量是 manually assignment。在你的情况下不需要依赖 属性

如果你想像下图那样使用,你的 属性 timeSpent 必须依赖 属性。