mar*_*tin 4 data-binding format wpf xaml converter
我经常在我的wpf项目中使用文本框,这些文本框绑定到datetime-propertys.我想将日期格式化为德语格式dd.MM.yyyy.目前我使用自编写的转换器,我可以提供所需的日期格式.
例如这样:
<TextBox Name="Date" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type prj:MyBar}}, Path=Date, Converter={StaticResource dateConverter}, ConverterParameter=dd.MM.yyyy}" />
Run Code Online (Sandbox Code Playgroud)
转换器唯一能做的就是调用ToString(string formatString)DateTime 的-method.
是否有一种"更聪明"的方式来格式化具有数据绑定的日期.最好的是如果不需要编写C#代码.也许microsoft-libs中有任何现有的类可以进行数据绑定的日期转换,但我还没有找到它.
如果有任何建议会很好,
问候,马丁
.NET 3.5 SP1具有StringFormatter.
<TextBox Name="Date" Text="{Binding Date, StringFormat='{}{0:MM/dd/yyyy}'}"/>
Run Code Online (Sandbox Code Playgroud)
结果:02/02/2010
<TextBox Name="Date" Text="{Binding Date, StringFormat='{}{0:D}'}"/>
Run Code Online (Sandbox Code Playgroud)
结果:2010年2月2日,星期二
但结果也可能因系统默认的DateTime格式而异.