Вопрос

In XAML I have something like this:

Text="{Binding my_date, StringFormat=\{0:ddd M/d/yy\}}"

currently it outputs some thing like Mon 12/23/13

I want it to honor the region and language settings date time format so if they pick a yy/mm/dd it should show the date in correct format. I know the param "d" should do that, from here: http://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx

But for some reason when it gets combined with the rest of the formatter as

Text="{Binding my_date, StringFormat=\{0:ddd d\}}"

Then it doesn't work correctly. What is the correct format I should use in this case?

Это было полезно?

Решение

Standard and custom strings can't be combined into a single string format. This would need to be done in two steps:

var weekday = my_date.ToString("ddd");
var day = my_date.ToString("d");
var both = string.Format("{0} {1}", weekday, day);

In XAML, this would be more verbose, but the following should work (this is untested):

<TextBlock.Text>
    <MultiBinding StringFormat="\{0\} \{1\}">
          <Binding Path="my_date" StringFormat="ddd" />
          <Binding Path="my_date" StringFormat="d" />
    </MultiBinding>
</TextBlock.Text>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top