我有一個DataGrid,其ItemsSource被設置爲一個DataTable。 DataTable具有DateTime類型的列,並且如果特定單元格中的日期爲特定值,我想顯示信息文本(即「N/A」)。轉換DataGrid單元格內容
我的第一個想法是以某種方式將細胞內容綁定到自身,並使用一個轉換器,但我似乎無法使其正常工作,並且似乎應該有更好的方法。
此外,DataGrid和DataTable都是動態生成的,因此必須在後面的代碼中完成。
這是我最初試圖代碼:
// Create a new DataGridCellStyle
Style myStyle = new Style();
myStyle.TargetType = typeof(DataGridCell);
// Create the binding
Binding myBinding = new Binding();
myBinding.RelativeSource = RelativeSource.Self;
myBinding.Converter = new DateTimeToStringConverter();
// Add the Content setter
Setter mySetter = new Setter();
mySetter.Property = ContentProperty;
mySetter.Value = myBinding;
myStyle.Setters.Add(setter);
// Set the Style and ItemsSource
myDataGrid.CellStyle = myStyle ;
myDataGrid.ItemsSource = myDataTable.DefaultView;
DateTimeToStringConverter確實實現的IValueConverter,但我猜的問題在於某處的結合,因爲DateTimeToStringConverter顯示DataGrid中時,實際上從未被調用。