[英]How to add a comma to a custom DateTime StringFormat in WPF
[英]Custom DateTime stringformat in WPF
我不能讓我的自定義DateTime字符串格式在我的綁定中工作。 我希望格式為“mmmm,yyyy”(例如“2012年6月”)。
以下不起作用。 我得到一個短日期格式(m / d / yyyy)。
<TextBlock Text="{Binding ElementName=ThisWindow,
Path=Date,
StringFormat={}{0:MMMM\, yyyy}"/>
我考慮使用轉換器,但我更喜歡純XAML方法。
編輯:
為清楚起見,我有一個帶有依賴屬性的Window
Date
類型的DateTime
。 在我的XAML中,我將窗口命名為“Thiswindowow”。
編輯2:
我回顧了我的實際代碼,我有一個Label
,而不是TextBlock
。 我將其更改為TextBlock,它工作正常。
<Label Content="{Binding ElementName=ThisWindow,
Path=Date,
StringFormat={}{0:MMMM\, yyyy}"/>
任何人都知道為什么它不適用於Label
?
謝謝。
ContentControls
具有ContentStringFormat
屬性 ,該屬性會覆蓋原始格式。
(當我看到你的問題時,我預計這實際上是問題但是最初發現一個TextBlock
很驚訝)
你的月需要大寫:
{Binding Source={x:Static sys:DateTime.Now}, StringFormat={}{0:MMMM\, yyyy}}
編輯:
Label問題可能是因為Label有Content而不是Text。
將Text =“{Binding ...}”更改為Content =“{Binding ...}”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.