2
votes

I'm trying to get my TreeView to bind to a Dictionary that will be updated. I've implemented INotifyProperyChangedHandler on the data type itself, but will this effect the treeview at all?

What I'm aiming for is:

-FolderName
--->Item1
--->Item2
-FolderName
--->Item1

This is my View:

<UserControl.Resources>
    <HierarchicalDataTemplate x:Key="ChildTemplate" >
        <TextBlock FontStyle="Italic" Text="{Binding Path=m_Items}" />
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate x:Key="NameTemplate" 
        ItemsSource="{Binding Path=Value}" 
        ItemTemplate="{StaticResource ChildTemplate}">
        <TextBlock Foreground="White" Text="{Binding m_Name}" FontWeight="Bold" />
    </HierarchicalDataTemplate>
</UserControl.Resources>

<TreeView x:Name="NewTree" ItemsSource="{Binding m_FolderList, UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{StaticResource NameTemplate}">
    </TreeView>

My ViewModel:

    public Dictionary<UInt16, Folder> m_FolderList
    {
        get { return Manager.Instance.GetFolderDirectory(); }
    }

My Folder class:

public class Folder
{
     public m_Name { get; set; }
     public ObservableCollection<String> m_Items { get; set; }
}

What I'm getting is a blank treeview that never updates. Whenever I add a new item to the "FolderDirectory" in that singleton manager instance, I do an OnPropertyChanged call. Which works for anything else I've been binding to that dictionary or Folder item.

I was trying to follow this, but how the binding observes that it automatically childrens the "FamilyMembers" list escapes me because nowhere do you explicitly tell the XAML template that it should bind itemsource to that collection. http://www.wpf-tutorial.com/treeview-control/treeview-data-binding-multiple-templates/

EDIT: The control is definitely getting all the values from my singleton. I just was playing around with the naming of the Binding Path and when I accidentally set the TreeView to bind to m_FolderList.Value (dictionaries have Values, not just Value), it gave me as many binding errors in the console as there were Items in the dictionary.

EDIT2:

public List<Folder> m_FolderList
{
  get
  {
    List<Folder> list = new List<Folder>();
    list.AddRange(Manager.Instace.GetFolderDirectory().Values);
    return list;
  }
}

If I do this, instead of Dictionary, the first Level of information appears... Which is a nuisance

2

2 Answers

0
votes

Try this

    <Window.Resources>

    <DataTemplate x:Key="secondLevel">
        <TextBlock Text="{Binding}"/>
    </DataTemplate>

    <HierarchicalDataTemplate  x:Key="topLevel" ItemsSource="{Binding Value.m_Items}" ItemTemplate="{StaticResource secondLevel}">
        <TextBlock Text="{Binding Value.m_Name}">
        </TextBlock>
    </HierarchicalDataTemplate>

</Window.Resources>
<StackPanel>
    <TreeView x:Name="FolderTree" ItemsSource="{Binding m_FolderList}" ItemTemplate="{StaticResource topLevel}">
    </TreeView>
</StackPanel>

output enter image description here

0
votes
public List<Folder> m_FolderList
{
        get
        {

                List<Folder> list = new List<Process>();
                list.AddRange(Manager.Instance.GetFolderDirectory().Values);
                return list;
        }
}

I hate doing this, but its the only method that worked here with the Heirarchical template. I've used a binding to dictionary Path=Value before numerous times but it just refused to work in this instance with many attempts. Its not the best answer, but its the only working one I have at the moment.