2

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.

J King
  • 4,108
  • 10
  • 53
  • 103

1 Answers1

1

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

Stipo
  • 4,566
  • 1
  • 21
  • 37
  • awesome, moved the style inside of a datagrid.resources and everything works exactly as I want. Thanks a lot! – J King Apr 28 '13 at 19:59
  • Stipo, The datatemplate does not change when I select a different vlaue in the combobox that binds to the object property tracking_action.chrCode as shown above. Do you know why? – J King Apr 28 '13 at 21:23
  • I am not sure why it doesn't work, you should try to debug it. Try with suggestions from here http://stackoverflow.com/questions/337023/how-to-detect-broken-wpf-data-binding – Stipo Apr 30 '13 at 23:05