views:

166

answers:

2

Hi,

I have a well defined tree repository. Where I can rename items, move them up, down, etc. Add new and delete.

The data is stored in a table as follows:

Index    Parent   Label    Left    Right
1        0        root     1       14
2        1        food     2       7
3        2        cake     3       4
4        2        pie      5       6
5        1        flowers  8       13
6        5        roses    9       10
7        5        violets  11      12

Representing the following tree:

                       (1) root (14)
         (2) food (7)                  (8) flowers (13)
  (3) cake (4)  (5) pie (6)    (9) roeses (10) (11) violets (12)

and displayed visually as follows:

root
  food
    cake
    pie
  flowers
    roses
    violets

Now, my problem is how to represent this in a bindable way, so that a TreeView can handle all the possible data changes? Renaming is easy, all I need is to make the label an updatble field. But what if a user moves flowers above food? I can make the relevant data changes, but they cause a complete data change to all other items in the tree. And all the examples I found of bindable hierarchies are good for non static trees..

So my current (and bad) solution is to reload the displayed tree after relocation change.

Any direction will be good.

Thanks

A: 

Don't use a TreeView use a 3rd party graph control. A brief google search came up with several sites, check out this one for example GoSilverlightDemo check out the OrgChartEditor sample, you can drag nodes around and change the links.

Aviad P.
Nice control, but this is for an opensource project. So i cant use it
Am
+1  A: 

Have you tried using a rather simple structure including an ObservableCollection<> for the child nodes? Something that would implement the interface:

interface ITreeNode<T>
    where T : ITreeNode
{
    string Name { get; set; }
    ObservableCollection<T> Children { get; }
}

Then you can use a HeirarchicalDataTemplate for your TreeView's XAML:

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

You'll have to work out drag-and-drop yourself, but that should get you started to display everything.

I just recently had to do the above myself, and this CodeProject article helped me, in case you need any more: http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx

Update:
Since you currently have everything in a table format and I'll assume you won't want to revise your default data structure, use an IValueConverter to get the items you want. This code is untested, but I'd try something similar to the following:

{Binding Index, Converter={StaticResource myConverter}}

When you declare myConverter, give it a reference to your tabular data. Using the index as the value and having the reference to the table, you can return from the convert method an IEnumerable with the appropriate results.

Let me know if you need more detail.

mattdekrey