2012-11-19 76 views
0

我在XAML中爲我的日期有以下datagrid列。我需要格式化爲「MM/dd/yyyy」格式。在C中設置DataGrid StringFormat#

<sdk:DataGridTextColumn Width="Auto" Binding="{Binding MyDate, StringFormat='MM/dd/yyyy'}" Header="Date" /> 

如何在C#中重現字符串格式化部分?我想在代碼中將新列添加到我的Datagrid中。

DataGridTextColumn textColumn = new DataGridTextColumn(); 
textColumn.Header = "Date"; 
textColumn.Binding = new Binding("MyDate"); 
textColumn.CanUserReorder = true; 
//???? 
this.MyDataGrid.Columns.Add(textColumn); 

回答

1

這樣的事情呢?

var binding = new Binding("MyDate"); 

binding.StringFormat = "MM/dd/yyyy"; 
textColumn.Binding = binding; 
1

什麼

textColumn.Binding = new Binding("MyDate") {StringFormat = "MM/dd/yyyy"};