如何删除动态创建的树视图项

本文关键字:创建 视图 动态 何删除 删除 | 更新日期: 2023-09-27 18:24:59

我有一个WPF应用程序,它允许用户在treeview中动态创建子项。这些子项是从一个单独的窗口创建的,并添加到主窗口中。我还想实现一个删除方法,即从一个单独的窗口中删除主窗口子项。

这些是我的想法和一些代码:

//Okay button -- Delete sub-items in main window TreeView
private void button2_Click(object sender, RoutedEventArgs e)
{
    //Query for Window1
    var mainWindow = Application.Current.Windows
        .Cast<Window1>()
        .FirstOrDefault(window => window is Window1) as Window1;
    //Name of header that needs to be located
    string header = textBox1.Text;
    //While treeview from main window contains subitems
    while (!mainWindow.treeView.Items.IsEmpty)
    {
        //Find TreeView subitem with matching header
        //? - not sure on code
        //Delete TreeView subitem
        //I'm guessing it has something to do with
        //mainWindow.treeView.Items.Remove(At?)....
    }
}

我的评论表明了我不确定的地方。我已经正确地查询了我的mainWindow,并为我想要查找的头设置了一个字符串值。我已经设置了一个循环来搜索我的treeview,但不知道完成任务的确切代码。请给我看我应该使用的代码。

根据答案修订代码

我正在尽力理解你的答案。我已经在评论中解释了我要做的事情。我认为我走在了正确的轨道上,但在尝试使用RemoveAll时遇到了一个小的编译器错误。我需要包括某种using resourceDictionary吗?

代码修订

非常感谢你一直支持我。编译器仍然会在调用RemoveAll时出错。

//Okay button -- Delete location and corrusponding block
    private void button2_Click(object sender, RoutedEventArgs e)
    {
        //Close Delete Window
        this.Close();
        //Query for Window1
        var mainWindow = Application.Current.Windows
            .Cast<Window1>()
            .FirstOrDefault(window => window is Window1) as Window1;
        //Name of header that needs to be located
        string header = textBox1.Text;
        //Treeview under operation from main window
        TreeViewItem items = mainWindow.treeViewItem;
        //Delete corresponding node
        RemoveAll(items, p => string.Equals(p.Header, header));
    }
    //REMOVE ALL METHOD - for use with button_click ^
    public void RemoveAll(ItemCollection items, Predicate<TreeViewItem> isValid)
    {
        for (int i = items.Count - 1; i >= 0; i--)
        {
            TreeViewItem vItem = (TreeViewItem)items[i];
            if (isValid(vItem))
            {
                items.RemoveAt(i);
            }
            else
            {
                RemoveAll(vItem.Items, isValid);
            }
        }
    }

Buld错误

错误1:"的最佳重载方法匹配。。。(项目)…'有一些无效的参数。

错误2:参数"1":无法从System.Windows.Controls.TreeViewitem转换为"System.Windows.Controls.ItemCollection".

谢谢。

如何删除动态创建的树视图项

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        string header = txtRemove.Text; //name of Treeviewitem to delete
        TreeView view = trvView; //get from location
        RemoveAll(view.Items, p => string.Equals(p.Header, header));
    }
    public bool Remove(ItemCollection items, Predicate<TreeViewItem> isValid)
    {
        for (int i = items.Count - 1; i >= 0; i--)
        {
            TreeViewItem vItem = (TreeViewItem)items[i];
            if (isValid(vItem))
            {
                items.RemoveAt(i);
                return true;
            }
            else
            {
                bool isDeleted = Remove(vItem.Items, isValid);
                if (isDeleted)
                    return isDeleted;
            }
        }
        return false;
    }
    public void RemoveAll(ItemCollection items, Predicate<TreeViewItem> isValid)
    {
        for (int i = items.Count - 1; i >= 0; i--)
        {
            TreeViewItem vItem = (TreeViewItem)items[i];
            if (isValid(vItem))
            {
                items.RemoveAt(i);
            }
            else
            {
                RemoveAll(vItem.Items, isValid);
            }
        }
    }
}

///Xaml

<Window x:Class="TreeViewSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TreeView x:Name="trvView" HorizontalAlignment="Left" Height="215" Margin="68,35,0,0" VerticalAlignment="Top" Width="221">
            <TreeView.Items>
                <TreeViewItem Header="1">
                    <TreeViewItem Header="1/1"/>
                    <TreeViewItem Header="1/2"/>
                    <TreeViewItem Header="1/3"/>
                </TreeViewItem>
                <TreeViewItem Header="2">
                    <TreeViewItem Header="2/1"/>
                    <TreeViewItem Header="2/2"/>
                    <TreeViewItem Header="2/3"/>
                </TreeViewItem>
                <TreeViewItem Header="3">
                    <TreeViewItem Header="3/1"/>
                    <TreeViewItem Header="3/2"/>
                    <TreeViewItem Header="3/3"/>
                </TreeViewItem>
            </TreeView.Items>
        </TreeView>
        <Button  Content="Button" HorizontalAlignment="Left" Margin="333,155,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
        <TextBox x:Name="txtRemove" HorizontalAlignment="Left" Height="23" Margin="333,102,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    </Grid>
</Window>