0

I have a TreeView which is bound to a hierarchical collection of complex type.

<TreeView Name="treeCategories" SelectedValuePath="{Binding SelectedCategory, RelativeSource={RelativeSource AncestorType=UserControl}}">
        <HierarchicalDataTemplate DataType="{x:Type LocalCategories:BaseCategory}" ItemsSource="{Binding Subcategories}">
            <TextBlock Text="{Binding DisplayName}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

The hierarchical rendering now works fine, but I can't get the tree to pre-select a 'current item'. The current item is a dependency property of the User Control, defined as

public BaseCategory SelectedCategory { .. }

I have ensured that it has value, although I am not 100% sure when this value is set - before or after the tree is rendered? If it is after, then that would explain the problem.

I have tried:

SelectedValuePath="{Binding SelectedCategory, RelativeSource={RelativeSource AncestorType=UserControl}}"

and

SelectedValuePath="SelectedCategory"

but I can't get it to work.

How do I make the TreeView preselect a 'current item', that is a dependency property of the User Control in which the tree is defined?

Edit

I am thinking, maybe I can use an item style to check if the item's underlying object is equal to my 'selected object'.

<TreeView.ItemContainerStyle>
  <Style TargetType="TreeViewItem">
    <Style.Triggers>
      <DataTrigger Binding="{Binding SelectedCategory, RelativeSource={RelativeSource AncestorType=UserControl}}" Value="TreeViewItem.UnderlyingObject">
        <Setter Property="IsSelected" Value="True" />
      </DataTrigger>
    </Style.Triggers>
  </Style>
</TreeView.ItemContainerStyle>

In the statements above, how can I access the underlying BaseCategory object of TreeViewItem so that I can do comparison and use Data Triggers?

hyankov
  • 4,049
  • 1
  • 29
  • 46
  • `SelectedValuePath` serves a different purpose (which you can read about in its online documentation). Use `SelectedItem` instead. – Clemens Jan 24 '17 at 11:40
  • @Clemens, it _"has no accessible setter"_ , `Mode=OneWay` does not seem to help. – hyankov Jan 24 '17 at 11:49
  • Sorry, forgot that it's a readonly property in TreeView. [This question](http://stackoverflow.com/q/1000040/1136211) may help. – Clemens Jan 24 '17 at 11:53
  • @Clemens, I already went through it, thanks. I have updated my question. In the light of the new information, do you see an option? – hyankov Jan 24 '17 at 12:00

1 Answers1

0

You could bind the SelectedItem property of the TreeView to your SelectedCategory source property using a behaviour.

Please refer to the following blog post for an example of how to do this: https://blog.magnusmontin.net/2014/01/30/wpf-using-behaviours-to-bind-to-readonly-properties-in-mvvm/

<TreeView xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
          Name="treeCategories">
    <i:Interaction.Behaviors>
        <local:TreeViewSelectedItemBlendBehavior
                    SelectedItem="{Binding SelectedCategory, RelativeSource={RelativeSource AncestorType=UserControl}}" />
    </i:Interaction.Behaviors>
    <TreeView.Resources>
        ...
    </TreeView.Resources>
</TreeView>
mm8
  • 163,881
  • 10
  • 57
  • 88