ItemsSource가 DataTable로 설정된 DataGrid가 있습니다. 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 실제로 호출되지 않기 때문에 나는이 문제를 추측하고있어, 어딘가 바인딩에있다 :
여기에 내가 처음 시도 코드입니다.