tags:

views:

217

answers:

3

Hi,

I have a structure of geography objects:

Country
Areas,
Provinces,
Cities
and Hotels

Country has areas, areas has provinces, provinces has cities, and cities has hotels. Whne I'll click City node I wanna to get logical path eg: France,Provanse,SomeProvince,Montpellier,Grand Hotel.

Each class has fields: name, code nad listOf...

Treeview works great, but this method not:

 private void structureTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
if (structureTree.SelectedItem is Hotel)
            {
                objectCode = ((Hotel)structureTree.SelectedItem).Code;
                TreeViewItem item = e.OriginalSource as TreeViewItem;
                DependencyObject parent = VisualTreeHelper.GetParent(item);
                dlgEditHotel(objectCode, structureTree.Parent.ToString());
            }
          }

`**structureTree.SelectedItem as TreeViewItem **`

gives me null, when I'll click at some area, province,city or hotel

A: 

you could store names of tree items in their Tag or somewhere, and at SelectedItemChanged recursively traverse the tree up to the topmost item and build up the pathstring.

Axarydax
yes,but what if user will click at item, from another node and different type ...fro example. my pathstring looks like France,Provanse,SomeProvince , and he will click at germany/ bayern (this node has been expanded before) ??
that does not change things - you'd still need to build the path from the lowest item (clicked on "bayern" -> bayern's *parent* node is "germany", you'd build a string "germany\bayern"
Axarydax
that's what I want ! :) but how to get germany when I'll click Bayern ??
you need to make a function that will accept the SelectedItem and it will recursively call itself with SelectedItem's parent as an argument and passing out the built path string. Read up on recursive functions and you will surely get it.
Axarydax
ok, I know, but what is the code to get value Name from parent ??
you have to store the Name there yourself when the tree is being filled with data. How do you fill the tree?
Axarydax
A: 

you can read DataContext property of each TreeviewItem and will get the item(Country, Areas, Provinces, Cities, etc.). Read the Name(for example) and append into the path string recursively while traversing through the tree.

viky
A: 

Hi,

I have created a treeview as follows:

<TreeView Name="tree">
            <TreeViewItem Header="Country" Selected="GetName">
                <TreeViewItem Header="Areas" Selected="GetName">
                    <TreeViewItem Header="Provinces" Selected="GetName">
                        <TreeViewItem Header="Cities" Selected="GetName">
                            <TreeViewItem Header="Hotels" Selected="GetName">
                            </TreeViewItem>
                        </TreeViewItem>
                    </TreeViewItem>
                </TreeViewItem>

            </TreeViewItem>

        </TreeView>

Now GetName method would do something like this:

private void GetName(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)sender;
            name = item.Header + "." + name;
            if (!(item.Parent is TreeViewItem))
            {
                MessageBox.Show(name);
            }

        }

In the end the MessageBox would Show name as

Country.Areas.Provinces.Cities.Hotels.

I hope this is what you wanted.

Archie