dob_xml

Hi,

I have a ListView and associated GridView. How can I format the items in the columns to be 1,200,300 instead of 1200300 or change the format of a date

XAML

<ListView Margin="17,117,11,61" Name="listView1" ></ListView>

Code Behind

listView1.ItemsSource = list; //an ObservableCollection

GridView myGridView = new GridView();

myGridView.AllowsColumnReorder = true;

GridViewColumn gvc1 = new GridViewColumn();

gvc1.DisplayMemberBinding = new Binding("FundedExposure");

gvc1.Header = "Funded Exposure";

myGridView.Columns.Add(gvc1);

listView1.View = myGridView;

Thanks



Re: Windows Presentation Foundation (WPF) format items in GridView ListView - thousands seperator/date format etc.

WPCoder

Posted on my web site in early 2007:

How to display a Formatted Number


If you're doing this in code though, you'll need to create the binding differently:

Binding b = new Binding("FundedExposure");
b.Converter = new ToStringFormatConverter(); // create one and reuse this though as there's no reason to create more than one of these!
gvc1.DisplayMemberBinding = b;






Re: Windows Presentation Foundation (WPF) format items in GridView ListView - thousands seperator/date format etc.

dob_xml

Thank you for that.

For others here it is

Create the Class

[ValueConversion(typeof(object), typeof(string))]

public class NumberToStringFormatConverter : IValueConverter

{

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

{

string fmt = parameter as string;

if (!string.IsNullOrEmpty(fmt))

{

return string.Format(culture, fmt, value); }

else

{

return value.ToString();

}

}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

{

return null;

}

}

GridViewColumn gvc3 = new GridViewColumn();

Binding b = new Binding("FundedExposure");

b.Converter = new NumberToStringFormatConverter();

b.ConverterParameter = "{0[:#],#}";

gvc3.DisplayMemberBinding = b;

gvc3.Header = "Funded Exposure";

myGridView.Columns.Add(gvc3);