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:
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)
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With