简体   繁体   中英

How do I format the contents of a DataGrid cell as a Date? as Currency?

I have a Silverlight DataGrid that's being populated with different types of data for each column. I'm trying to figure out how to format some of the contents of the DataGrid 's cells, specifically for dates and formatting.

I have a date column that's currently displaying like: 3/11/2010 12:00:00 AM . I would rather it display like 3/14/2010 .

I have a number column that's currently displaying like: 51.32 . I'd rather it display as currency like $51.32 .

I'm not sure how I can go about doing this. I'd prefer to do it in XAML instead of C#, but both solutions are fine.


For reference, here's my XAML so far:

    </data:DataGridTextColumn>

        <data:DataGridTextColumn Header="Payee" 
                                 Binding="{Binding Payee}"/>
        <data:DataGridTextColumn Header="Category"
                                 Binding="{Binding Category}"/>
        <data:DataGridTextColumn Header="Memo" 
                                 Binding="{Binding Memo}"/>
        <data:DataGridTextColumn Header="Inflow" 
                                 Binding="{Binding Inflow}"/>
        <data:DataGridTextColumn Header="Outflow" 
                                 Binding="{Binding Outflow}"/>
    </data:DataGrid.Columns>

If you have the possibility to wait for the release of Silverlight 4, possibly at Mix10 next week. It has some new features in the binding, like formatting. With Silverlight 4 you can do stuff like:

<TextBox Text="{Binding ReleaseDate, StringFormat='MMM dd, yyyy', 
                    Mode=TwoWay}" />

Shawn Wildermuth has a nice overview of the new features on his blog .

You can use a IValueconverter based on string.Format and bind it in XAML.

Check out how to do it here

Yet another article to address the same

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM