C# WPF treeview filled with recursive built list<> add item at position
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
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.
精彩评论