Silverlight XAML TextBlock当前日期 [英] Current Date in Silverlight XAML TextBlock
问题描述
TextBlock
来显示今天的日期和时间,而不用在C#中编码它。我尝试了许多不同的变化,没有运气。 TextBlock Text ={Source = Date,Path = Now,StringFormat ='dd / MM / yyyy'}
我知道我可能只需要设置一个属性 MyDate
并绑定到那个,但为什么我不能直接绑定到 DateTime.Now
属性?
Silverlight中的绑定需要一个Source对象或一个Dependency对象。从该源对象可以绑定到属性(因此根据定义绑定到实例成员)或依赖属性。
由于 DateTime.Now
是一个静态属性,您不能直接在Silverlight中绑定它,因此需要一些代码。最好的办法是使用代码: -
- 确保您可以在XAML中表达所需的许多
- 以尽可能的解耦方式执行此操作。
因此,我们可以分析我们需要两个
- 将DateTime的静态成员作为某个对象的实例属性
- 有一些方法将DateTime格式化为所需的输出。
要处理第一个项目,我将创建一个 StaticSurrogate
class,我将为我们需要访问的静态属性创建实例属性: -
public class StaticSurrogate
{
public DateTime Today {get {return DateTime.Today; }}
public DateTime Now {get {return DateTime.Now; }}
}
现在我们需要一种格式化日期时间的方法。价值转换器是这项工作的正确工具,大量借鉴了这个 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中,首先我们在资源中需要这些类的实例: -
< UserControl.Resources>
< local:StaticSurrogate x:Key =Static/>
< local:FormatConverter x:Key =Formatter/>
< /UserControl.Resources>
现在我们可以连接 TextBlock
-
< TextBlock Text ={Binding Today,Source = {StaticResource Static},
Converter = {StaticResource Formatter},ConverterParameter ='dd MMM yyy'}/>
请注意,这种方法具有以下优点: -
- 我们不需要向放置TextBlock的UserControl添加代码,也不需要随意转动任何数据上下文。
- 静态资源可以被放置在App.Resources中,这样可以使TextBlock的创建完全独立于不必为UserControl添加任何东西。
- 用于显示日期的格式可以独立修改。
- 访问其他静态属性可以轻松地添加到
StaticSurrogate
类中。
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'}"
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?
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.
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:-
- ensure as much of what you need can be expressed in XAML
- to do so in an as de-coupled manner as possible.
Hence we can analyse that we need two things.
- Expose the static members of DateTime as instance properties of some object
- Have some way to format the DateTime to a desirable output.
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; } }
}
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();
}
}
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>
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:-
- 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屋!