Reputation: 9558
Situation:
I have a DataGrid
where the items can belong to different categories, say A, B and Custom. Each Category has its own RowDetails
template. The category of an item can be changed, and when this happens I want to change the template as well, if necessary. The ViewModel behind is the same, I just change the interface elements (for example, in template A I have a TextBlock
, while in template B I have a TextBox
, both with a Binding
to the same property in the VM).
What I have done so far:
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Control x:Name="RowDetails" Focusable="False" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding TemplateID, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" Value="0">
<Setter TargetName="RowDetails" Property="Template" Value="{StaticResource TemplateA}" />
</DataTrigger>
<DataTrigger Binding="{Binding TemplateID, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" Value="1">
<Setter TargetName="RowDetails" Property="Template" Value="{StaticResource TemplateB}" />
</DataTrigger>
<DataTrigger Binding="{Binding TemplateID, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" Value="2">
<Setter TargetName="RowDetails" Property="Template" Value="{StaticResource TemplateCustom}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
The problem:
When I change the category of an item, I can see that the template changes, but the entire row gets the ValidationErrorTemplate (the red border, with exclamation mark). This happens even if the templates are empty! Seems to me that just changing the template makes the whole thing to explode.
What am I doing wrong? Thanks!
Upvotes: 1
Views: 1685
Reputation: 9558
After a lot of try-and-retry, I found the problem to be on a ComboBox inside my templates: as stated here, order in the properties of a ComboBox matters. Putting SelectedValue
before ItemsSource
did the trick for me, and the approach using the Triggers
works like charm.
Nevertheless, I'm still shocked by such solution. I'll mark this as the answer, but I'm still open to suggesions.
Upvotes: 1