Use a property within an ItemsSource object as the property to display

时间:2015-11-12 11:46:50

标签: c# wpf xaml mvvm datagrid

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).
Rows are highlighted but the SourceTable is shown in a single column.

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:
Resulting view

0 个答案:

没有答案