views:

183

answers:

2

My ListView is petty simple:

<ListView ItemsSource="{Binding Path=ActiveCounters}">
    <ListView.View>
        <GridView>
            <GridViewColumn  Header="Name" DisplayMemberBinding="{Binding Path=Name}" />
            <GridViewColumn  Header="Value"  DisplayMemberBinding="{Binding Path=Value}" />
            <GridViewColumn  Header="As Of Date"  DisplayMemberBinding="{Binding Path=AsOfDate}" />
            <GridViewColumn  Header="Duration"  DisplayMemberBinding="{Binding Path=Duration}" />
            <GridViewColumn  Header="Last Modified Date"  DisplayMemberBinding="{Binding Path=Timestamp}" />
        </GridView>
    </ListView.View>
</ListView>

What I want to do is:

  • Format "Value" using the built-in format "D0"
  • Format "AsOfDate" and "Last Modified Date" using the custom string "MMM d hh:mm:ss tt"
  • Format "Duration" with a function defined as "String DurationString(TimeSpan)
+1  A: 

StringFormat can be added to your binding statement. For example

<GridViewColumn
   Header="As Of Date"
   DisplayMemberBinding="{Binding Path=AsOfDate, StringFormat={}{0:MMM d hh:mm:ss tt}}" />

See this post for more usage examples

qntmfred
Well that handles two out of the three cases. But what about calling a custom function?
Jonathan Allen
+1  A: 

For Value and AsOfDate columns use StringFormat attribute - a new feature of WPF 3.5 SP1. More about it here:

http://blogs.msdn.com/llobo/archive/2008/05/19/wpf-3-5-sp1-feature-stringformat.aspx

http://elegantcode.com/2009/04/07/wpf-stringformat-in-xaml-with-the-stringformat-attribute/

If you want to call a custom function on a bound value, then implement a value converter for that.

http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

You can call your custom function from convert method.

Miroslav Popovic