How can this be an overflow exception...?
<DataTemplate x:Key="ElementTemplate">
<StackPanel Orientation="Horizontal">
<StackPanel.Style>
<Style TargetType="{x:Type StackPanel}">
<Style.Triggers>
<DataTrigger Binding="{Binding Converter={StaticResource TypeConv}}" Value="{x:Type models:GroupModel}">
<Setter Property="Margin" Value="5 0 0 0"></Setter>
<Setter Property="DataContext" Value="{Binding Model}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<TextBlock Text="{Binding Name}"></TextBlock>
...
<StackPanel/>
<DataTemplate/>
For the reason: this is a template (with at least 25 UI contros) that normally needs Model A as datatype. the DataTemplate
is a ListvVew
ItemTemplate
. But the datatype can be of type Model B. Model B has a property called 'Model', which is of type Model A.
So instead of copy pasting the whole block template and use style triggers or DataTemplate
selectors, I just want to change the DataContex
t (from "{Binding}" to "{Binding Model}")
anyone has some suggestions, a solution?
Thx!
EDIT: the Converter
returns the type of the incoming value (the data object itself). that way i can know when Model B is using the template and so to change the DataContext.
A work-around I would suggest to avoid possible recursion between setting the data context and triggering the DataTrigger
:
Have both ModelA
and ModelB
implement a common interface called IListViewModel
for example with a single property getter:
public interface IListViewModel
{
ModelA Model {get;}
}
Then, ModelA
's implementation will return this
, while ModelB
's implementation returns this.ModelA
The DataTemplate
simply binds to the .Model
of whichever view model it's given.