C# 实时时钟的格式化问题

C# 实时时钟的格式化问题,c#,wpf,C#,Wpf,尝试格式化某个类返回的值,该类在发生更改时更新应用程序资源 这是我的密码: public class NotifyingDateTime : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private DateTime _now; public NotifyingDateTime() { _now = DateTime.No

尝试格式化某个类返回的值,该类在发生更改时更新应用程序资源

这是我的密码:

public class NotifyingDateTime : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private DateTime _now;

    public NotifyingDateTime()
    {
        _now = DateTime.Now;
        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromMilliseconds(2000);
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();
    }
    public string formated
    {
        get 
        { 
            DateTime datenow = Now;
            //Format the datatime
            string format = "dd MMM, yyyy - h:mm:s tt";
            string formatted = datenow.ToString(format);
            return formatted;
        }
    }
    public DateTime Now
    {
        get { return _now; }
        private set
        {
            _now = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Now"));
        }
    }
    void timer_Tick(object sender, EventArgs e)
    {
        Now = DateTime.Now;
    }
}
这是应用程序资源中的绑定:

{Binding Source={StaticResource NotifyingDateTime}, Path=formated}
当应用程序第一次运行时,格式化工作正常,但是因为我引用的是
格式化的
,而不是
现在的
,所以绑定没有注意到更新,因为更新发生在
现在的


最好的方法是什么?我怎样才能让一个股票行情器用我想要的当前日期时间格式更新一个应用程序资源

因为您已经绑定了
格式化的
属性,所以现在在
上发出更改通知
不会强制绑定更新。因此,在更改值后,在
格式化的
上发出更改通知,绑定可能会将其更新到UI

改变

PropertyChanged(this, new PropertyChangedEventArgs("Now")); 


哦,谢谢!我还以为那是在做别的事呢!lol@MartynBall,当您现在在
上使用转换器时
有必要在同一时间发出通知。但您的方法是使用另一个属性来执行格式化,因此需要对绑定属性发出更改通知,而不是源属性。最后但并非最不愉快的编码:)
PropertyChanged(this, new PropertyChangedEventArgs("formated"));