WPF HiercharchicalDataTemplate.DataType: How to react on interfaces?

Posted by David Schmitt on Stack Overflow See other posts from Stack Overflow or by David Schmitt
Published on 2008-11-03T15:54:10Z Indexed on 2010/04/02 8:13 UTC
Read the original article Hit count: 422

Filed under:
|
|
|

Problem

I've got a collection of IThings and I'd like to create a HierarchicalDataTemplate for a TreeView. The straightforward DataType={x:Type local:IThing} of course doesn't work, probably because the WPF creators didn't want to handle the possible ambiguities.

Since this should handle IThings from different sources at the same time, referencing the implementing class is out of question.

Current solution

For now I'm using a ViewModel which proxies IThing through a concrete 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; }
}

<!-- is never applied -->
<HierarchicalDataTemplate DataType="{x:Type local:IThing}">

<!-- is applied, but looks strange -->
<HierarchicalDataTemplate
    DataType="{x:Type local:IThingViewModel}"
    ItemsSource="{Binding Thing.SomeThings}">
    <TextBox Text="{Binding Thing.SomeString}"/>
</HierarchicalDataTemplate>

Question

Is there a better (i.e. no proxy) way?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about template