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
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