I would like to bind a list of ViewModels to a DataGrid
and then use a property on the ViewModel to display in the contents of the DataGrid
.
Suppose I have the following ViewModel:
public class TableWrapperViewModel
{
public object SourceTable { get; set; } //An unknown type, auto generate the columns.
public bool HasBeenDealtWith { get; set; } //Used to control the row style.
}
In the View I have a DataGrid
bound to an ObservableCollection<TableWrapperViewModel> Tables
:
<DataGrid ItemsSource="{Binding Tables}">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding HasBeenDealtWith}" Value="True">
<Setter Property="Background" Value="#BCFF77"/>
</DataTrigger>
<DataTrigger Binding="{Binding HasBeenDealtWith}" Value="False">
<Setter Property="Background" Value="#FF7777"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
The result can be seen below. The rows are highlighted but the SourceTable
is shown as a single column (as expected).
Is there a way I could display only the SourceTable
property in the DataGrid
whilst maintaining access to HasBeenDealtWith
for styling purposes? The DataGrid
must be able to generate the columns dynamically for the SouceTable
object.
The result should look like the following but with the highlighting still in place: