Silverlight XAML TextBlock中的当前日期

Ton*_*ith 8 silverlight xaml binding date textblock

我来自Flex,您可以在花括号内做任何事情.我试图在TextBlock没有用C#编码的情况下显示今天的日期和时间.我尝试了以下许多不同的变化而没有运气.

TextBlock Text="{Source=Date, Path=Now, StringFormat='dd/MM/yyyy'}"
Run Code Online (Sandbox Code Playgroud)

我知道我可能只是设置一个属性MyDate并绑定到那个,但为什么我不能直接绑定到DateTime.Now属性?

Ant*_*nes 14

Silverlight中的绑定需要Source对象或Dependency对象.从该源对象,您可以绑定到Properties(因此根据定义,您绑定到实例成员)或依赖属性.

由于DateTime.Now是静态属性,因此无法直接在Silverlight中绑定它,因此需要一些代码.下一个最好的事情是使用代码: -

  • 确保您需要的内容可以在XAML中表达
  • 以尽可能解耦的方式这样做.

因此,我们可以分析我们需要两件事.

  1. 将DateTime的静态成员公开为某个对象的实例属性
  2. 有一些方法可以将DateTime格式化为理想的输出.

为了处理第一项,我将创建一个StaticSurrogate类,在那里我将为我们需要访问的静态属性创建实例属性: -

public class StaticSurrogate
{
    public DateTime Today { get { return DateTime.Today; } }
    public DateTime Now { get { return DateTime.Now; } }
}
Run Code Online (Sandbox Code Playgroud)

现在我们需要一种格式化日期时间的方法.价值转换器是这项工作的正确工具,大量借鉴Tim Heuer博客: -

public class FormatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (parameter != null)
        {
            string formatterString = parameter.ToString();

            if (!String.IsNullOrEmpty(formatterString))
            {
                return String.Format(culture, String.Format("{{0:{0}}}", formatterString), value);
            }
        }

        return (value ?? "").ToString();
    }

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

有了这两个类,我们现在可以在Xaml中完成其余的工作,首先我们需要在我们的资源中使用这些类的实例:

<UserControl.Resources>
    <local:StaticSurrogate x:Key="Static" />
    <local:FormatConverter x:Key="Formatter" />     
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

现在我们可以连线TextBlock: -

<TextBlock Text="{Binding Today, Source={StaticResource Static},
    Converter={StaticResource Formatter}, ConverterParameter='dd MMM yyy'}" />
Run Code Online (Sandbox Code Playgroud)

请注意,此方法具有以下优点: -

  • 我们不需要向放置TextBlock的UserControl添加代码,也不需要摆弄任何数据上下文.
  • 静态资源可以放在App.Resources中,这将使TextBlock的创建完全独立于必须向UserControl添加任何其他内容.
  • 用于显示日期的格式可以单独修改.
  • 可以轻松地将更多静态属性的访问权添加到StaticSurrogate类中.


nyx*_*tom 7

即使你可以在Silverlight的XAML中声明DateTime.Now(因为你可以在WPF中 - http://soumya.wordpress.com/2010/02/12/wpf-simplified-part-11-xaml-tricks/),你有你的时间不会更新的问题.如果您使用本地计时器在第二个更新,您可以确保您的时间也将更新.

public class LocalTimer : INotifyPropertyChanged
{
    private DispatcherTimer timer;

    public LocalTimer()
    {
        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1.0);
        timer.Tick += new EventHandler(TimerCallback);
        this.TimeFormat = "hh:mm:ss";
        this.DateFormat = "dddd, MMMM dd";
    }

    private void TimerCallback(object sender, EventArgs e)
    {
        PropertyChanged(this, new PropertyChangedEventArgs("FormattedDate"));
        PropertyChanged(this, new PropertyChangedEventArgs("FormattedTime"));
    }

    public bool Enabled
    {
        get { return this.timer.IsEnabled; }
        set { if (value) this.timer.Start(); else this.timer.Stop(); }
    }

    public string FormattedDate { get { return DateTime.Now.ToString(this.DateFormat); } set {} }
    public string FormattedTime { get { return DateTime.Now.ToString(this.TimeFormat); } set{} }

    public string TimeFormat { get; set; }
    public string DateFormat { get; set; }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}
Run Code Online (Sandbox Code Playgroud)

在xaml ala中声明一个这样的实例:

<local:LocalTimer x:Key="theTime" Enabled="True" />
Run Code Online (Sandbox Code Playgroud)

并使用绑定来确保始终反映您的时间.

<TextBlock Text="{Binding Source={StaticResource theTime}, Path=FormattedDate, Mode=OneWay}" x:Name="TodaysDate" />
<TextBlock Text="{Binding Source={StaticResource theTime}, Path=FormattedTime, Mode=OneWay}" x:Name="CurrentTime" />
Run Code Online (Sandbox Code Playgroud)