绑定到DateTime.Now。更新值

时间:2010-07-28 15:56:59

标签: wpf binding

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

 Text="{Binding Source={x:Static System:DateTime.Now},StringFormat='HH:mm:ss tt'}"

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

4 个答案:

答案 0 :(得分:23)

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

这是使用INotifyPropertyChanged的'Ticker'类的a link,因此它会自动更新。以下是该网站的代码:

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}"/>

<强>声明:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

现在可行:

<TextBox Text="{Binding Source={StaticResource ticker}, Path=Now, Mode=OneWay}"/>

答案 1 :(得分:2)

对于Windows Phone,您可以使用此代码段

public Timer()
{
    DispatcherTimer timer = new DispatcherTimer();
    timer.Interval = TimeSpan.FromSeconds(1); // 1 second updates
    timer.Tick += timer_Tick;
    timer.Start();
}

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

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

public event PropertyChangedEventHandler PropertyChanged;

我改编了m-y的代码。希望这个也有用。

答案 2 :(得分:1)

您需要制作一个每秒更新文本框的计时器。

答案 3 :(得分:0)

实际上,做到这一点的“规范”方法是设置DispatcherTimer

但是,您也可以使用情节提要和伪造的转换器来做到这一点,例如:

        

    <Storyboard x:Key="clockStory" Duration="0:0:2" RepeatBehavior="Forever">
        <StringAnimationUsingKeyFrames
            Storyboard.TargetName="clock"
            Storyboard.TargetProperty="(Label.Tag)">
            <DiscreteStringKeyFrame KeyTime="0:0:0" Value="Let's force binding" />
            <DiscreteStringKeyFrame KeyTime="0:0:1" Value="..to change back and forth" />
        </StringAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>

<Window.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
        <BeginStoryboard Storyboard="{StaticResource clockStory}"/>
    </EventTrigger>
</Window.Triggers>

<Grid>
    <Label x:Name="clock" Content="{Binding ElementName=clock, Path=Tag, Converter={StaticResource conv}}"/>
</Grid>

..其中以下是转换器

public class AnythingToCurrentTimeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return DateTime.Now.ToString("HH:mm:ss");
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

享受!