Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Multibinding string format date

Tags:

wpf

xaml

I'm trying to combine 2 fields of information in my grid by using a Multibinding, the multibinding is working fine but I'm having problems when I try to start formating 1 of the fields which is a date in this binding.

The 2 fields are Users Initials i.e. EGJ and the entry date hoping to achieve a combined field looking like "EGJ - 01/01/2011"

Below is where I'm with my existing XAML

<tk:DataGridTextColumn.Binding>
  <MultiBinding StringFormat=" {0} - {}{1:dd/MM/yyyy}">
    <Binding Path="UserInitials" />
    <Binding Path="EntryDate" />
  </MultiBinding>
</tk:DataGridTextColumn.Binding>   

Any help or pointers are most appreciated

like image 956
Emlyn Avatar asked Jan 27 '11 15:01

Emlyn


2 Answers

Couldn't see the wood for the trees

Simply removing the empty braces solved my problem.

<tk:DataGridTextColumn.Binding>
  <MultiBinding StringFormat=" {0} - {1:dd/MM/yyyy}">
    <Binding Path="UserInitials" />
    <Binding Path="EntryDate" />
  </MultiBinding>
</tk:DataGridTextColumn.Binding>

Thanks to everyone who took the time to look.

like image 121
Emlyn Avatar answered Oct 04 '22 02:10

Emlyn


Unless you intend to have a leading space in the formatted value you should use this binding instead:

<tk:DataGridTextColumn.Binding> 
  <MultiBinding StringFormat="{}{0} - {1:dd/MM/yyyy}"> 
    <Binding Path="UserInitials" />
    <Binding Path="EntryDate" />
  </MultiBinding>
</tk:DataGridTextColumn.Binding>

If the StringFormat starts with a left brace { the XAML parser require you to escape it using a pair of braces {}. Otherwise the parser gets confused because braces also are used in the syntax of markup extensions.

Details are found in the XAML documentation for {} Escape Sequence / Markup Extension.

Perhaps you had the escape sequence correctly placed in the format string initially and the moved things around resulting in the empty pair of braces at the wrong place?

like image 33
Martin Liversage Avatar answered Oct 04 '22 02:10

Martin Liversage