绑定到DateTime.Now.更新值

iLe*_*ing 12 wpf binding

好吧,我需要将DateTime.Now绑定到TextBlock,我使用了:

 Text="{Binding Source={x:Static System:DateTime.Now},StringFormat='HH:mm:ss tt'}"
Run Code Online (Sandbox Code Playgroud)

现在,如何强制更新?它是控制加载的时间,不会更新它...

m-y*_*m-y 23

编辑(我没有考虑他想要自动更新):

这是一个使用INotifyPropertyChanged的'Ticker'类的链接,因此它会自动更新.这是该网站的代码:

namespace TheJoyOfCode.WpfExample
{
    public class Ticker : INotifyPropertyChanged
    {
        public Ticker()
        {
            Timer timer = new Timer();
            timer.Interval = 1000; // 1 second updates
            timer.Elapsed += timer_Elapsed;
            timer.Start();
        }

        public DateTime Now
        {
            get { return DateTime.Now; }
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Now"));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}


<Page.Resources>
   <src:Ticker x:Key="ticker" />
</Page.Resources>

<TextBox Text="{Binding Source={StaticResource ticker}, Path=Now, Mode=OneWay}"/>
Run Code Online (Sandbox Code Playgroud)

宣布:

xmlns:sys="clr-namespace:System;assembly=mscorlib"
Run Code Online (Sandbox Code Playgroud)

现在这将工作:

<TextBox Text="{Binding Source={StaticResource ticker}, Path=Now, Mode=OneWay}"/>
Run Code Online (Sandbox Code Playgroud)

  • 错误.这没有用.(这正是他写的) (2认同)