2
votes

This one is a bit complicated. I'm trying to create a usercontrol which has a treeview and a few other controls, to create a reusable control that will be useful for other implementations.

The problem that I'm having is that I cannot figure out how to insert a HierarchicalDataTemplate defined outside the control into the treeview that is inside the control.

Outside the control here is my WPF

<Grid>
    <Grid.Resources>
        <HierarchicalDataTemplate x:Key="HierarchicalDataTemplate" ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Path=Name}"/>
        </HierarchicalDataTemplate>
    </Grid.Resources>

    <masterTreeUserControl:MasterTreeUserControl 
        HierarchicalDataTemplate="{StaticResource HierarchicalDataTemplate}"
        ItemsSource="{Binding Path=SelectiveListViewModel.Items, UpdateSourceTrigger=PropertyChanged}"
        SelectedItem="{Binding Path=SelectiveListViewModel.SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
        EnableAdd="False"
        ItemType="{x:Type viewmodels:LocationItem}"
        />
</Grid>

The MasterTreeUserControl has a HierarchicalDataTemplate DependencyProperty

HierarchicalDataTemplateProperty = DependencyProperty.Register("HierarchicalDataTemplate",typeof(HierarchicalDataTemplate),typeof(MasterTreeUserControl));


private static readonly DependencyProperty HierarchicalDataTemplateProperty;
    public HierarchicalDataTemplate HierarchicalDataTemplate
    {
        get
        {
            return (HierarchicalDataTemplate)GetValue(HierarchicalDataTemplateProperty);
        }
        set
        {
            SetValue(HierarchicalDataTemplateProperty, value);
        }
    }

And so far the Treeview inside the control looks like this.

<TreeView Name="ItemListView"
              Grid.Row="2"
              Margin="0,5,0,0"
              ItemsSource="{Binding Source={StaticResource ItemsCvs}}">

In other implementations of a treeview what I would normally do is HierarchicalDataTemplate in a fashion similar to this.

<TreeView.Resources>
    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
        <TextBlock Text="Hello"/>
    </HierarchicalDataTemplate>
</TreeView.Resources>

However, as the HierarchicalDataTemplate is inside a dependency property, I need to somehow bind it into the Treeview. I've had a look a round the internet (and will continue doing so) but can't having found anything relevent.

How can you inject a dependency property containing the HierarchicalDataTemplate into the treeview?

1

1 Answers

1
votes

Following should work:

  1. Add PropertyChangedCallback to your HierarchicalDataTemplateProperty.
  2. In the handler, add template to control resources:

    private static void OnTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var This = d as MasterTreeUserControl;
        var template = e.NewValue as HierarchicalDataTemplate;
    
        if(template != null)
        {
            This.ItemListView.Resources[new DataTemplateKey(template.DataType)] = template;
        }
    }
    

The only issue is that your HierarchicalDataTemplate has to have DataType set to your templated type. You should enforce that in some way (validation etc..)