How to use StringFormat in XAML elements?

Edward Tanguay picture Edward Tanguay · Mar 26, 2009 · Viewed 80.6k times · Source

I'm deep in a XAML stack of elements binding to orders.

The order date displays as e.g. "12/31/2008 12:00:00 AM".

I want it to display as e.g. "31.12.2008".

How can I do this? I have seen other stackoverflow questions mention StringFormat but they use multibinding in ways that I can't get to work.

Here is the kind of syntax I would like (this is pseudocode), simply specifying StringFormat where you need it, is this possible somehow?

<StackPanel>
    <ListView ItemsSource="{Binding Orders}">
        <ListView.View>
            <GridView>
                <GridViewColumn 
                    Header="Order ID" 
                    DisplayMemberBinding="{Binding Path=OrderID}"
                    StringFormat="{}{1:dd.MM.yyyy}"/>
                <GridViewColumn 
                    Header="Order Date" 
                    DisplayMemberBinding="{Binding Path=OrderDate}"/>
            </GridView>
        </ListView.View>
    </ListView>
</StackPanel>

Answer

Razzie picture Razzie · Mar 26, 2009

I haven't tested it, but I think this should work:

<GridViewColumn
    Header="Order Date"
    DisplayMemberBinding=
        "{Binding Path=OrderDate, StringFormat='{}{0:dd.MM.yyyy}'}"/>