Problem
I have a collection IThingand I would like to create HierarchicalDataTemplatefor TreeView. Simple DataType={x:Type local:IThing}, of course, does not work, perhaps because the creators of WPF did not want to handle possible ambiguities.
Since this should process IThingfrom different sources at the same time, a reference to the implementation class is out of the question.
Current solution
I am currently using ViewModel, which proxies IThing through a specific implementation:
public interface IThing {
string SomeString { get; }
ObservableCollection<IThing> SomeThings { get; }
// many more stuff
}
public class IThingViewModel
{
public IThing Thing { get; }
public IThingViewModel(IThing it) { this.Thing = it; }
}
<HierarchicalDataTemplate DataType="{x:Type local:IThing}">
<HierarchicalDataTemplate
DataType="{x:Type local:IThingViewModel}"
ItemsSource="{Binding Thing.SomeThings}">
<TextBox Text="{Binding Thing.SomeString}"/>
</HierarchicalDataTemplate>
Question
Is there a better way (i.e. without a proxy)?
source
share