目前在Silverlight XAML的TextBlock日期 [英] Current Date in Silverlight XAML TextBlock

查看:146
本文介绍了目前在Silverlight XAML的TextBlock日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从未来的Flex哪里可以做几乎花括号内的任何东西。我试图让的TextBlock 不只是编码在C#中,显示今天的日期和时间。我已经试过,没有运气以下的许多不同的变化。

I am coming from Flex where you can do just about anything inside of curly braces. I am trying to get a TextBlock to display today's Date and Time without just coding it in C#. I have tried many different variations of the following with no luck.

TextBlock Text="{Source=Date, Path=Now, StringFormat='dd/MM/yyyy'}"

我知道我很可能只是设置一个属性数值指明MyDate 并绑定到这一点,但我为什么不能直接绑定到 DateTime.Now 属性?

I know I could probably just set a property MyDate and bind to that but why can't I bind directly to the DateTime.Now property?

推荐答案

在Silverlight中绑定需要一个源对象或依赖对象。从源对象可以绑定到属性(因此,通过定义要绑定到实例成员)或依赖属性。

Binding in Silverlight requires a Source object or a Dependency object. From that source object you can bind to Properties (hence by definition you are binding to instance members) or Dependency Properties.

由于 DateTime.Now 是不能直接绑定到它在Silverlight静态属性,因此需要一些code。未来最好的事情就是用code到: -

Since DateTime.Now is a static property you cannot bind to it in Silverlight directly, hence some code is needed. The next best thing is to use code to:-


  • 确保尽可能多的你需要什么可以在XAML pssed前$ P $

  • 在作为去耦合方式可以这样做。

因此​​,我们可以分析,我们需要两样东西。

Hence we can analyse that we need two things.


  1. 揭露日期时间的静态成员的一些对象实例的属性

  2. 有办法的DATETIME格式到所需的输出。

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

To handle the first item I would create a StaticSurrogate class, where I would create instance properties for the static properties that we need access to:-

public class StaticSurrogate
{
    public DateTime Today { get { return DateTime.Today; } }
    public DateTime Now { get { return DateTime.Now; } }
}

现在,我们需要一种方法来格式化日期时间。值转换器是这个工作的工具,从这个<一个大量举债href=\"http://timheuer.com/blog/archive/2008/07/30/format-data-in-silverlight-databinding-valueconverter.aspx\">Tim豪雅志: -

Now we need a way to format a Date time. A value converter is the right tool for this job, borrowing heavily from this Tim Heuer Blog :-

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();
    }
}

通过这两个类的地方,我们现在可以做其余的在XAML中,首先我们需要这些类的实例在我们的资源: -

With these two classes in place we can now do the rest in Xaml, first we need instances of these classes in our resources:-

<UserControl.Resources>
    <local:StaticSurrogate x:Key="Static" />
    <local:FormatConverter x:Key="Formatter" />     
</UserControl.Resources>

现在,我们可以连线了的TextBlock : -

Now we can wire up the TextBlock :-

<TextBlock Text="{Binding Today, Source={StaticResource Static},
    Converter={StaticResource Formatter}, ConverterParameter='dd MMM yyy'}" />

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

Note that this approach has the following advantages:-


  • 我们不需要code添加到其上的TextBlock放置在用户控件,我们也不需要反复折腾的任何数据的上下文。

  • 静态资源可以被放置在App.Resources这将使完全独立不必什么都添加到用户控件的创建TextBlock的。

  • 用于显示日期的格式可独立修改。

  • 要额外的静态属性访问可以很容易地添加到 StaticSurrogate 类。

  • we do not need to add code to the UserControl on which the TextBlock is placed, nor do we have to fiddle around with any data context.
  • The Static resources could be placed in the App.Resources which would make the creation of the TextBlock entirely independent of having to add anything else to the UserControl.
  • The formatting used to display the date can be independently modified.
  • Access to additional static properties can easily be added to the StaticSurrogate class.

这篇关于目前在Silverlight XAML的TextBlock日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆