views:

93

answers:

1

Hi, i have a TreeView which is binded trough a HierarchicalDataTemplate. Im listing there a List which contains a list of a directories and files.

private void getDirectoryList()
    {
         using (FileOperations fileOP = new FileOperations())
        {
            this.DokumentBrowser.ItemsSource = fileOP.list_directory(rpath); //Liste wird an den DokumentenBrowser gebunden
            selectedOrdner = (Ordner)DokumentBrowser.Items.GetItemAt(0);
            this.FileBrowser.DataContext = selectedOrdner.FileName;
        }
    }

The selectedOrdner buffers the Ordner Object which is actual selected by the user in the TreeView. If its empty it is set to the item at position 0 which is the root directory. When now the user puts a new file in the TreeView

runMethodtoCopyFile
                    getDirectoryList();
                    TreeViewItem tvi = (TreeViewItem)DokumentBrowser.ItemContainerGenerator.ContainerFromItem(DokumentBrowser.Items.CurrentItem);
                    tvi.IsExpanded = true;
                    tvi.Focus();

im runnning a method to copy the file and then im reading the new directory structure and want to set the expand property to true from the last treeviewitem which was selected. Because my TreeView was always showing me my rootnode when i put a new file or dir into it.

The Buffer of the selectedOrdner contains as long as im not putting a new file into it the Ordner Object. But when im running the method to copy a file it no longer contains an Object from type Ordner but treeviewitem instead.

I dont get it where it is changed from type Ordner to type TreeViewItem.

edit-----

The error always happens when the TreeView SelectedItemChanged happen.

        private void DokumentBrowser_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        selectedOrdner = (Ordner)DokumentBrowser.SelectedItem;
        FileBrowser.ItemsSource = selectedOrdner.FileName;
    }

When I try to add something to the rootnode it works fine it only are the nodes after the first where it starts to break.

edit2------ There was a problem when binding the treeview to a new itemsource that the selecteditemchange method has a disconnecteditem and that could not be castet to a (Ordner)

solved this by making all changes over an observable collection. So the TreeViewItem is Expanded is not longer needed

A: 

Bring XAML to light please! )) At the current state of inquiry it is hard to say something. May be problem in binding somewhere in your HierarchicalDataTemplate or it might be another piece of your codebehind.The only i can say now - you'd better check all your references from/to SelectedItem both in WPF XAML and C# code parts.

P.S.: Very strange by the way - usually people are confused with counterpart problem, how to get TreeViewItem for SelectedItem.

ProfyTroll