WPF DataGrid datatable binding

It drives me crazy. I create a DataGrid in the code and then bind it to a datatable. This is dynamic, and the rows and columns will be different each time you create a grid.

Basically, I go through my datatable and create DataGrid columns for each column, for example:

private static void CreateDataGridColumns(DataGrid datagrid, Document doc) { if (doc == null) return; //return datagrid.Columns.Clear(); foreach (var item in doc.Keys) { var column = new DataGridTemplateColumn { Header = item, CellTemplateSelector = new CustomRowDataTemplateSelector(), }; datagrid.Columns.Add(column); } } 

As you can see, I use a custom data template selector so that I can render the cell differently depending on its contents.

Here is the pattern selector

 public class CustomRowDataTemplateSelector : DataTemplateSelector { public override DataTemplate SelectTemplate(object item, DependencyObject container) { FrameworkElement element = container as FrameworkElement; var presenter = container as ContentPresenter; var gridCell = presenter.Parent as DataGridCell; if (element != null && item != null && gridCell != null) { var row = item as DataRow; if (row != null) { var cellObject = row[gridCell.Column.DisplayIndex]; //set template based on cell type if (cellObject is DateTime) { return element.FindResource("dateCell") as DataTemplate; } return element.FindResource("stringCell") as DataTemplate; } } return null; } } 

Here is my stringCell DataTemplate

 <DataTemplate x:Key="stringCell"> <StackPanel> <TextBlock Style="{StaticResource cellStyle}" Grid.Row="0" Grid.Column="0" Text="{Binding Converter={StaticResource cellConverter}}" /> </StackPanel> </DataTemplate> 

The problem is that the template selector is called for each cell (as expected), but I can’t determine which cell it is, so I don’t know how to set the text in the TextBlock. I would like to do something like this

 <DataTemplate x:Key="stringCell"> <StackPanel> <TextBlock Style="{StaticResource cellStyle}" Grid.Row="0" Grid.Column="0" Text="{Binding Path=Row[CellIndex], Converter={StaticResource cellConverter}}" /> </StackPanel> </DataTemplate> 

But for me there is nothing available for CellIndex. How can I do something like this where I can set Path = Row [CellIndex]

+4
source share
2 answers

You can try to create a binding in the code. Something like this should work

 var bind = new Binding(gridCell.Column.Header.ToString()) bind.Mode = BindingMode.TwoWay; bind.Source = row; BindingOperations.SetBinding(YourTextBlock, TextBlock.TextProperty, bind); 
0
source

Not sure what you are trying to achieve functionally. You can get away with this in code. Create a higher level class, CellClass, which has the DisplayValue property. With the implementation of the date and string. Associate the source with CellClass with Path = DisplayValue. You can even create a CellClasses list and bind to CellClass [0], CellClass [1] ... You know, this works the way I do it, but I'm not sure if it provides the functionality you are looking for.

  public abstract class CellClass { public abstract String DispValue { get; } } public class CellClassDate : CellClass { public override String DispValue { get ...; } public DateTime DateValue { get .. set ... } } public class CellClassString : CellClass { public override String DispValue { get ...; } public DateTime StringValue { get .. set ... } } 
-1
source

All Articles