WPF TreeView Items not selectable
Asked Answered
S

1

8

I am currently writing an Folder Browser Dialog in WPF. For displaying the Tree I use an TreeView:

<TreeView Name="FolderView" ItemsSource="{Binding DataTrees}" Grid.Row="0">
    <TreeView.ItemTemplate>
      <HierarchicalDataTemplate ItemsSource="{Binding Tree}">
      <TreeViewItem IsSelected="{Binding IsSelected, Mode=TwoWay}" IsExpanded="{Binding IsExpanded, Mode=TwoWay}" Header="{Binding Name}" HorizontalAlignment="Left"/>
      </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
  </TreeView>

Currently I have three Problems:

  1. You can't select an Item in the running Programm
  2. The Header is about two tabs to the right (not critical but ugly)
  3. 'IsExpanded' is only set when double clicking an item and not on clicking [+]

I don't know where the Problem is so please comment and I will update my Question!

EDIT: The Itemsource is a List Data Tree Class:

public class DataTree:INotifyPropertyChanged
{
  private string path;

  private string name;

  private ObservableCollection<DataTree> tree;

  private bool isSelected;

  private bool isExpanded;
}

(simple Code - Without Propertys and Implementation Of INotifyPropertyChanged)

Skep answered 23/9, 2014 at 8:19 Comment(1)
Modifying the ItemTemplate without knowing about the internal structure will cause many problems. You should use Expression Blend to view the internal structure of a TreeViewItem.Aubrette
B
13

Do not add TreeViewItem into ItemTemplate directly:

<TreeView Name="FolderView" ItemsSource="{Binding DataTrees}" Grid.Row="0">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">    
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
        </Style>    
    </TreeView.ItemContainerStyle>

    <TreeView.ItemTemplate>
      <HierarchicalDataTemplate ItemsSource="{Binding Tree}">
          <TextBlock Text="{Binding Name}"/>
      </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

As any ItemsControl, TreeView wraps its data items into item container (TreeViewItem in your case). Hence, things like selection and expansion should be set via ItemContainerStyle.

Bernadettebernadina answered 23/9, 2014 at 8:32 Comment(5)
I tried that before but the Setter Value Binding fails due data context Problems. "IsSelected" is a Property of an Item in the ItemSource of the TreeView and not an Property of the ViewModel.Skep
Obviously, you need IsSelected and IsExpanded in the data context of TreeViewItem. What kind of items are in your ItemsSource? And what is "ViewModel" in that case?Bernadettebernadina
Yes. The Itemsource is a List<DataTree> (I'm adding the code). The ViewModel is the ViewModel of the MVVM concept.Skep
The property doesn't exist in the DataContext ViewModel - as you assumed right I need the Data Context of the TreeViewItem, but I don't know how to achieve this.Skep
Sorry all my fault, there wasn't really a Problem, only ReSharper said there was one...Skep

© 2022 - 2024 — McMap. All rights reserved.