2
votes

I have a wpf application using MVVM principles. Within this application I have a datagrid where I want to have each row choose between one of 6 possible datatemplates based on the value of an underlying property in the object bound to the datagrid row.

GOAL: I do not want to use code behind and want to avoid using a datatemplateselector. I would like to use a datatrigger to select from different data templates for the DetailsTemplate of each row.

ATTEMPTED: I tried to define a datatrigger within the DataGrid as follows, but it does not work.

            <DataGrid Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="3" MinHeight="300"          
                    ItemsSource="{Binding TrackingCollection}"  
                    CanUserAddRows="False" CanUserDeleteRows="False"
                    SelectionMode="Single" SelectedItem="{Binding SelectedTracking, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                <Style TargetType="{x:Type DataGridRow}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding tracking_action.chrCode}" Value="RC">
                            <!-- RECIEVING -->
                            <Setter Property="DetailsTemplate" Value="{StaticResource FTC_TrackingReceivingDetailTemplate}" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding tracking_action.chrCode}" Value="SH">
                            <!-- SHIPPING -->
                            <Setter Property="DetailsTemplate" Value="{StaticResource FTC_TrackingShippingDetailTemplate}" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding tracking_action.chrCode}" Value="ST">
                            <!-- STOCKING -->
                            <Setter Property="DetailsTemplate" Value="{StaticResource FTC_TrackingStockingDetailTemplate}" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding tracking_action.chrCode}" Value="OR">
                            <!-- ORDERING -->
                            <Setter Property="DetailsTemplate" Value="{StaticResource FTC_TrackingOrderingDetailTemplate}" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding tracking_action.chrCode}" Value="RT">
                            <!-- RETURNING -->
                            <Setter Property="DetailsTemplate" Value="{StaticResource FTC_TrackingReturningDetailTemplate}" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding tracking_action.chrCode}" Value="TR">
                            <!-- TRANSFERING -->
                            <Setter Property="DetailsTemplate" Value="{StaticResource FTC_TrackingTransferingDetailTemplate}" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
                <DataGrid.Columns>
                   ''column definitions go here
                </DataGrid.Columns>
            </DataGrid>

ERROR: When I run the app I get the following error:

'Add value to collection of type 'System.Windows.Controls.ItemCollection' threw an exception.' Line number '137' and line position '35'.

This error goes away if I remove the Style tag/content from the datagrid definition above.

Can someone please help me define the row's datatemplate based on the value of a property within that row.

Thank you in advance.

1

1 Answers

1
votes

You should put Style into <DataGrid.Resources> under <DataGrid>, not directly under <DataGrid>.