Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 绑定到DateTime.Now。更新值_Wpf_Binding - Fatal编程技术网

Wpf 绑定到DateTime.Now。更新值

Wpf 绑定到DateTime.Now。更新值,wpf,binding,Wpf,Binding,嗯,我需要绑定DateTime。现在,对于文本块,我使用了: Text="{Binding Source={x:Static System:DateTime.Now},StringFormat='HH:mm:ss tt'}" 现在,如何强制它更新?这是加载控件并且不会更新它的时间 编辑(我没有说明他想自动更新): 下面是一个使用INotifyPropertyChanged的“Ticker”类的示例,它将自动更新。以下是网站上的代码: namespace TheJoyOfCode.WpfExa

嗯,我需要绑定DateTime。现在,对于文本块,我使用了:

 Text="{Binding Source={x:Static System:DateTime.Now},StringFormat='HH:mm:ss tt'}"
现在,如何强制它更新?这是加载控件并且不会更新它的时间

编辑(我没有说明他想自动更新):

下面是一个使用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}"/>
现在这将起作用:

xmlns:sys="clr-namespace:System;assembly=mscorlib"
<TextBox Text="{Binding Source={StaticResource ticker}, Path=Now, Mode=OneWay}"/>


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

对于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的代码。希望这一条也能有用。

事实上,实现这一点的“规范”方法是设置一个Dispatcher

但是,您也可以使用故事板和假转换器来完成此操作,如下所示:

享受吧

错。这没有帮助。(这正是他写的)
    <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();
    }
}