【问题标题】:Binding to DateTime.Now. Update the value绑定到 DateTime.Now。更新值
【发布时间】:2010-07-28 15:56:59
【问题描述】:

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

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

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

【问题讨论】:

    标签: wpf binding


    【解决方案1】:

    这是使用 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}"/>
    

    【讨论】:

      【解决方案2】:

      对于Windows Phone,你可以使用这个sn-p

      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 的代码。希望这个也能有用。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          确实,这样做的“规范”方式是设置 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();
              }
          }
          

          享受吧!

          【讨论】:

            猜你喜欢
            • 2022-10-06
            • 2014-11-05
            • 2017-07-14
            • 2020-12-08
            • 2014-12-02
            • 1970-01-01
            • 2019-01-13
            • 1970-01-01
            • 2014-08-26
            相关资源
            最近更新 更多