C# WPF 树视图填充递归构建列表<>在位置添加项目

发布于 2024-09-16 08:45:51 字数 3615 浏览 9 评论 0 原文

您好,我有一个 TreeView,其中充满了分层数据模板

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

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

用于获取对象的类看起来像这样

#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

它充满了文件夹及其子文件夹。例如,当根文件夹有 2 个子文件夹并且它们有 2 个子文件夹时,它看起来像这样 alt text

当我现在添加文件时或文件夹通过某些方法我想将新文件名或 Ordner 对象添加到我的集合中。如何找到 TreeView 中 SelectedItem 的位置? 每次更改时,我都会缓冲 TreeView selectedOrdner = (Ordner)DokumentBrowser.SelectedItem; 中的 SelectedItem,并将新文件名或 Ordner 对象添加到此项。 现在我怎样才能将新项目扔到列表中的正确位置?或用新值更新旧值。

我是否总是必须递归地浏览列表才能找到当前所在的对象?还是有更好更简单的方法?

--- 编辑

更改了我的 Ordner 类实现了一个可观察的集合并保存了作为它所保存的节点的根节点的对象,以便我可以删除它们。它可能不是最好的解决方案,但对我来说效果很好

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事与诗 2024-09-23 08:45:51

我不确定你在问什么;由于您拥有选定的项目,因此您可以用它执行各种操作,包括将子项插入其中。您必须提供有关您正在尝试执行的操作的更多详细信息。

如果您尝试添加所选项目的同级并使它们出现在树视图中,则可以尝试查看它们的不同数据结构,这将使有序插入变得非常容易和快速,例如 LinkedList。另一方面,我还建议您使用 ObservableCollection 来保存项目,因为它会自动反映用户界面的更改。您还可以在 ObservableCollection 中的特定索引处插入项目,所以我无法想象问题是什么。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文