Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
610
StringFormat, data annotation attribute DataFormatString and xamGrid
posted

Hi,

I am trying to come up with some generic way to apply Data Annotation attribute StringFormat to XAML:

[DisplayFormat(DataFormatString = "MM-dd-yyyy")]
public DateTime? Date { get; set; }

It does not work automatically. For regular data grid I can do this:

private void gridMessages_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 
       
{ 
           
DataGridBoundColumn obj = e.Column as DataGridBoundColumn; 
           
var attrs = Attribute.GetCustomAttributes(typeof(TestMessage).GetProperty(e.PropertyName)); 
           
var attr = attrs.FirstOrDefault(a => a is DisplayFormatAttribute) as DisplayFormatAttribute; 
           
if (obj != null && obj.Binding != null && attr != null && string.IsNullOrEmpty(obj.Binding.StringFormat)) 
           
{ 
                obj
.Binding.StringFormat = attr.DataFormatString; 
           
} 
       
}

But xamGrid does not have AutoGeneratingColumn event.

Any suggestion how to insert DataFormatString into data binding for xamGrid.

Or even better, maybe xamGrid can use somehow data annotation attributes on data model for data formating?

Thanks.

Vic

Parents Reply Children
No Data