DeclarableDataGrid will help you contol Windows Presentation Foundation DataGrid columns. It allows you to declare columns statically, or add them while running WPF application.
- Declare which properties of the object need to be added as DataGrid column
- Add other columns during runtime
- Bindings are supported
- Allow to edit cells of the DataGrid added via DeclarableData grid.
- Set DataGrid to automatically generate columns and bind the data source. Data source must be of type
DeclarableDataGridObservableCollection<T>where T is the type you want as a row in DataGrid.Tmust inherit from theDeclarableDataGrid.PropertyDescriptors.DeclarableColumnDataDescriptor
<DataGrid Name="ExampleDataGrid" ItemsSource="{Binding ExampleCollection}" AutoGenerateColumns="True" AutoGeneratingColumn="ExampleDataGrid_AutoGeneratingColumn"></DataGrid>
- In the event handler use default create function
private void ExampleDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
DeclarableDataGridBuilder.DefaultCreateDeclarableDataGrid(e);
}
- Or create
DeclarableDataGridBuilderin the window constructor
_declarableDataGridBuilder = new DeclarableDataGridBuilder();
_declarableDataGridBuilder.ConfigureColumnTemplates(Resources)
.ForColumnUseTemplate("DynamicColumn1", "ColumnTemplate")
.ForColumnUseTemplate("PersonData", "PersonDataColumnTemplate");
- In the event handler of the
AutoGeneratingColumnevent include the helper function from the DeclarableDataGrid
private void ExampleDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
_declarableDataGridBuilder.OnDeclarableColumnGenerated(e);
}
- Select properties which should be used by DataGrid in a fluent manner:
ExampleCollection.UsePropertyAsColumn(x => x.PersonId, x => x.WithDisplayName("Id"));
ExampleCollection.UsePropertyAsColumn(x => x.Name, x => x.WithDisplayName("Name"));
ExampleCollection.UsePropertyAsColumn(x => x.LastName, x => x.WithDisplayName("Last Name"));
ExampleCollection.UsePropertyAsColumn(x => x.BirthDate, x => x.WithDisplayName("Birth Date"));
- Or add DataGrid properties later in a loop for example:
ExampleCollection.UseDynamicColumn<int>("DynamicColumn1", x => x.WithDisplayName("Dynamic column 1"));
ExampleCollection.UseDynamicColumn<double>("DynamicColumn2", x => x.WithDisplayName("Dynamic column 2"));
ExampleCollection.UseDynamicColumn<string>("DynamicColumn3", x => x.WithDisplayName("Dynamic column 3"));
- Dynamic columns can be set using indexer operator on the object (type of the set property must match the declared type):
ExampleCollection.Add(new PersonDataGridItem
{
PersonId = 1,
BirthDate = new DateTime(1990, 1, 1),
Name = "John",
LastName = "Smith",
HiddenColumn = "SSN",
["DynamicColumn1"] = 1,
});