views:

291

answers:

1

Hi I have a TreeView which is filled with a hierarchicaldatatemplate

<TreeView Name="DokumentBrowser" ItemTemplate="{StaticResource HierachrTree}"

<HierarchicalDataTemplate x:Key="HierachrTree" 
    DataType="{x:Type src:Ordner}" 
    ItemsSource="{Binding UnterOrdner}">
                    <TextBlock Text="{Binding OrdnerName}"/>
        </HierarchicalDataTemplate>

The Class which is used to get the Objects looks like this

#region OrdnerClass
public class OrdnerListe : ObservableCollection<Ordner>
{
    protected override void InsertItem(int index, Ordner item)
    {
        base.InsertItem(index, item);
        this[index].PropertyChanged += new PropertyChangedEventHandler(OrdnerListe_PropertyChanged);
    }

    void OrdnerListe_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(
          NotifyCollectionChangedAction.Reset));
    }
}


public class Ordner : INotifyPropertyChanged
{
    #region fields
    private string _name, _path; //Ordnername
    private Ordner _pordner; //Parent Ordner Objekt
    private OrdnerListe _uordner; //Liste aus Ordner Objekten
    private File_Liste _filename; // Liste der Dateien die in einem Ordner Liegen
    #endregion

    #region INotifyPropertyMember
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,new PropertyChangedEventArgs(info));
        }
    }
    #endregion

    #region konstruktor
    public Ordner()
    {
        _uordner = new OrdnerListe();
        _filename = new File_Liste();
    }
    #endregion

    #region properties
    public string OrdnerName
    {
        get { return _name; }
        set
        {
            if (value != this._name)
            {
                this._name = value;
                NotifyPropertyChanged("OrdnerName");
            }
        }
    }

    public string OrdnerPfad
    {
        get { return _path; }
        set
        {
            if (value != this._path)
            {
                this._path = value;
                NotifyPropertyChanged("OrdnerPfad");
            }
        }
    }

    public Ordner ParentDir
    {
        get { return _pordner; }
        set
        {
            if (value != this._pordner)
            {
                this._pordner = value;
                NotifyPropertyChanged("ParentDir");
            }
        }
    }

    public OrdnerListe UnterOrdner
    {
        get { return _uordner; }
        set
        {
            if (value != this._uordner)
            {
                this._uordner = value;
                NotifyPropertyChanged("UnterOrdner");
            }
        }
    }

    public File_Liste FileName
    {
        get { return _filename; }
        set
        {
            if (value != this._filename)
            {
                this._filename = value;
                NotifyPropertyChanged("FileName");
            }
        }
    }
    #endregion
}
#endregion

It is filled with folders and their subfolders. For Example when the root folder has 2 subfolders and they have 2 subfolders it will look like this alt text

When i now add files or folders through some methods i want to add the new filenames or Ordner objects to my collection. How can i find the position where the SelectedItem from the TreeView is? Im Buffering the SelectedItem from the TreeView selectedOrdner = (Ordner)DokumentBrowser.SelectedItem; every time when it changed and add to this item the new filenames or Ordner Objects. How can I now throw the new Item to the right position in my list ? or update the old with the new values.

Do I always have to go recursive through my list to find the Object where im momentarily? or exists there a better and easier way?

--- edit

changed my Ordner Class implemented an observable collection und save the Object that is the root node of the nodes that it holds so i can remove them. its maybe not the best solution but it works fine for me

+2  A: 

I'm not sure about what you're asking; since you have the selected item, you can do all sorts of things with it, including inserting children into it. You'll have to provide a bit more details about what you are trying to do.

If you're trying to add siblings of the selected item and have them appear in the tree view, you can try looking at different data structures for them, which would make ordered insertion very easy and fast, such as LinkedList. On the other hand, I also suggest you use ObservableCollection to hold items, since it will automatically reflect the changes in the user interface. You can also insert items at a specific index in the ObservableCollection, so I can't imagine what the problem would be.

Alex Paven
+1 for ObservableCollection
Avatar
I had a major thinking problem there, its ok now when im adding items to my list. But how can i destroy or remove the item which is the SelectedItem from my treeview ? without knowing which is the item that holds it in the list. I hope you understand
Mark
If you want to go with MVVM best practices, I think the easiest way is to expose the collections as `ICollectionView`s; the ICollectionView is automatically synchronized with the selected item in an ItemsControl - so the selected item is available in the ViewModel as the ICollectionView's CurrentItem.Going the code-behind route, the selected _TreeViewItem_ can be obtained directly from the TreeView, and you can get the bound item by calling the treeview's ItemContainerGenerator.ItemFromContainer method. Let me know if you need further clarification.
Alex Paven
Hi there i went through the code behind route ;) i updated my class and added an observable collection and saved the parent node of my tree so i can also get from the selected item to the root which holds the nodes. Maybe not the best solution but it works fine for me;) now i have my little own explorer;) thanks for the hints and tips
Mark